/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型
* @param m int整型
* @return int整型
*/
struct ListNode * BuyNode(int n)//创建节点和成环
{
struct ListNode *phead=NULL;
struct ListNode *ptail=NULL;
int N=1;
while(n--){
struct ListNode *newnode=(struct ListNode *)malloc(sizeof(struct ListNode));
newnode->val=N++;
newnode->next=NULL;
if(phead==NULL)
{
phead=ptail=newnode;
}
else{
ptail->next=newnode;
ptail=newnode;
}
}
ptail->next=phead;
return ptail;//返回尾节点
}
int ysf(int n, int m ) {
// write code here
if(n == 1)
{
return n;
}
struct ListNode * tail = BuyNode(n);
struct ListNode * cur = tail->next;
int N = 1;
while(cur->next != cur)
{
if(N == m)
{
tail->next = cur->next;
cur = tail->next;
N = 1;
}
N++;
cur = cur->next;
tail = tail->next;
}
return cur->val;
}
【刷题】牛客网 NC132 环形链表的约瑟夫问题
2024-04-21 18:38:07 26 阅读