约瑟夫问题(已完成但是有最后一个测试条件没有通过)

代码如下,辛苦看下那种情况没有考虑。


#include<stdio.h>
#include <stdlib.h>
typedef struct number{
    int value;
    struct number *next;
}num;
int main()
{
    int n,m;
    scanf("%d %d",&n,&m);
//创建包含n个人的链表
    num *head=NULL;
    for(int i=1;i<n+1;i++){
        num *p=(num*)malloc(sizeof(num));
        p->value=i;
        p->next=NULL;
        num *last=head;
        if (head==NULL)
            head=p;
        else{
            while (last!=NULL&&last->next!=NULL) {
                last=last->next;
            }
            last->next=p;
        }
    }
//将链表的末尾与头节点连接在一起。
    num *p=head;
    if (head==NULL) {
        head->next=head;
    }
    else{
        while (p!=NULL&&p->next!=NULL) {
            p=p->next;
        }
        p->next=head;
    }
//循环删除输入的第m个元素
    num *point=head;
    int i=1;
    while(point->next!=point){
        if (i==m-1) {
            num *p1=point->next;
            point->next=p1->next;
            free(p1);
            i=0;
        }
        point=point->next;
        i++;
    }
    printf("%d",point->value);
}