/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @param m int整型 * @return int整型 */ typedef struct ListNode ListNode; //创建新节点 ListNode* BuyNode(int x) { ListNode* newnode = (ListNode*)malloc(sizeof(ListNode)); newnode->val = x; newnode->next = NULL; return newnode; } //循环链表的创建 ListNode* List(int n) { ListNode* phead = BuyNode(1); ListNode* ptail = phead; for(int i = 2; i <= n; i++) { ptail->next = BuyNode(i); ptail = ptail->next; } ptail->next = phead; return phead; } int ysf(int n, int m ) { // write code here //如果m为1,直接返回1 if(m == 1) { return 1; } ListNode* phead = List(n); ListNode* pcur = phead; ListNode* prev = NULL; //定义count记录报数 int count = 1; while(pcur->next != pcur) { if(count == m) { prev->next = pcur->next; free(pcur); pcur = prev->next; count = 1; } else { prev = pcur; pcur = pcur->next; count++; } } return pcur->val; }