本文最后更新于571 天前,其中的信息可能已经过时,如有错误请发送邮件到1986413837@qq.com
采取递归调用的方式来反转链表
(PS:感觉自己对递归调用的返回理解不清楚~ )
以第三次返回为例子:
head->next=4
转换步骤:
5- >4<-3
5- >4- >3
5- >4- >3- >NULL
一定要搞清楚 我不想做半吊子了

设置好递归出口 如果head==NULL||head->next==NULL即可return head
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
{
return head;
}
ListNode*newHead=reverseList(head->next);
head->next->next=head;
head->next=NULL;
return newHead;
}
};