C语言循环语句转换成递归

while (x != x->next)
{
for (i = 1; i < M; i++) x = x->next;
x->next = x->next->next; N--;
}
这段代码是在algorithms in c中,删除列表的第M个元素,直到剩一个元素,转换成递归函数怎么写?

判断x->next == NULL

void delete(int M,int *N)
{
for (i = 1; i < M; i++) x = x->next;
x->next = x->next->next; N--;
if(x == x->next)
return;
delete(M,N);
}

这个不需要转换成递归吧
要换成递归,,参考

int df(struct xxxx x,int M,int *N)
{
    int i;
    if(x == x->next)
        return 0;
    for (i = 1; i < M; i++)
       x = x->next;
    x->next = x->next->next; *N--;
    df(x,M,N);
}

调用方式  df(x,M,&N);

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

while (x->next != NULL)
{
   x = x->next;
   x->next = x->next->next; N--;
}

这样

while (x->next != NULL)
{
x = x->next;
x->next = x->next->next; N--;
}