提交记录
执行结果:
解题思路:
【206.反转链表-哔哩哔哩】 https://b23.tv/2Lb5qG6
- 首先,从head起申请左、右指针,分别指向head和head->next;
- 将左指针的next指向空;
- 右指针指向左指针(为了不让右指针的next丢失,之前需要先将右指针的next用临时指针tmp指向left->next);
- 然后,左、右指针以及临时指针tmp平移;
语言:C++
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL ||head->next==NULL){
return head;
}
ListNode*l=head;
ListNode*r=head->next;
head->next=NULL;
while(r){
ListNode*tmp=r->next;
r->next=l;
l=r;
r=tmp;
}
return l;
}
};