OJ链接:反转链表
方法一:就地三指针转方向

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next==NULL)
return head;
ListNode *n1,*n2,*n3;
n1=NULL;
n2=head;
n3=n2->next;
while(n2)
{
n2->next=n1;
n1=n2;
n2=n3;
if(n3)
n3=n3->next;
}
return n1;
}
};
方法二:头插法
- 取节点新插到新链表
/**
* Definition for singly-linked list.
* struct ListNode {
*

本文介绍了如何反转单链表,包括两种方法:就地三指针转换方向和头插法。通过这两种方法,可以实现链表节点的反转操作。
最低0.47元/天 解锁文章
4493

被折叠的 条评论
为什么被折叠?



