1.双指针法反转链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* tmp;
ListNode* cur=head;
ListNode* pre = NULL;
while(cur)
{
tmp=cur->next;
cur->next=pre;
pre=cur;
cur=tmp;
}
return pre;
}
};
2.递归法反转链表
class Solution{
public:
ListNode*reverse(ListNode*pre, ListNode*cur)
{
if(cur==NULL) return pre;
ListNode*temp = cur->next;
cur->next=pre;
//可以和双指针法的代码进行对比,如下递归的写法,其实就是做了这两步
//pre = cur;
//cur=temp;
return reverse(cur,temp);
}
ListNode*reverseList(ListNode*head){
//和双指针法初始化是一样的逻辑
//ListNode*cur=head;
//ListNode*pre = NULL;
return reverse(NULL,head);
}
}