思路1:
创建新的链表,遍历原链表,将原链表的节点进行头插到新链表中。
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* next = NULL;
struct ListNode* new_head = NULL;
if (head == NULL ||head->next == NULL) // 空链或只有一个结点,直回头
{
return head;
}
while (head != NULL) {
next=head->next;
head->next = new_head;
new_head = head;
head = next;
}
return new_head;
}
思路2:
创建三个节点,依次进行挪动。
struct ListNode* reverseList(struct ListNode* head) {
if(head==NULL||head->next==NULL)
{
return head;
}
struct ListNode* n1,*n2,*n3;
n1=NULL,n2=head,n3=n2->next;
while(n2)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3!=NULL)
n3=n3->next;
}
return n1;
}
一张图搞懂上面的核心代码:

如果我的文章对你有帮助,期待你的三连!








![[Windows] 油.管视频下载神器 Gihosoft TubeGet Pro v9.3.88](https://i-blog.csdnimg.cn/direct/6e3b3785bb1f4c158f6d87d2380c5bb0.png)




![二叉搜索树的实现[C++]](https://i-blog.csdnimg.cn/direct/736dbe00da704034a33908d6642f92f9.jpeg#pic_center)





