单链表的实现:https://blog.csdn.net/weixin_41892460/article/details/82855823
1.三指针法
void ReverseList(pList* pplist)
{
pNode cur = NULL;
pNode prev = NULL;
pNode tmp = NULL;
assert(pplist);
if (*pplist == NULL || (*pplist)->next == NULL)
{
return;
}
prev = *pplist;
cur = prev->next;
tmp = cur->next;
while (cur)
{
cur->next = prev;
prev = cur;
cur = tmp;
if (tmp)
tmp = tmp->next;
}
(*pplist)->next = NULL;
*pplist = prev;
}
2.头插法
void ReverseList(pList* pplist)
{
assert(pplist);
if (*pplist == NULL || (*pplist)->next == NULL)
{
return;
}
pNode cur = NULL;
pList newnode = NULL;
while (*pplist)
{
cur = *pplist;
*pplist = cur->next;
cur->next = newnode;
newnode = cur;
}
*pplist = newnode;
}