反转单个链表很容易,下面的代码可以正常工作。
void reverse_list (SLINK list)
{
SLINK tmp = list->next;
NODE *cur = NULL;
list->next = NULL;
for (; NULL != tmp;)
{
cur = tmp;
tmp = tmp->next;
cur->next = list->next;
list->next = cur;
}
}
如何反转循环单链表?我将代码调整为此
SLINK reverse_list (SLINK rear)
{
NODE *tag = rear->next;
SLINK tmp = rear->next;
NODE *cur = NULL;
rear->next = NULL;
for (; NULL != tmp; )
{
cur = tmp;
tmp = tmp->next;
cur->next = rear;
rear->next = cur;
}
rear = tag;
return rear;
}
但它不起作用,我以为运行这个函数后,循环列表将是非循环的,实际上,转储它后,我发现它仍然是一个循环列表。一定是我逻辑错了,请指正。