方法1:
非递归
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* pre=head;
head=head->next;
ListNode* next=head->next;
pre->next=NULL;
while(next)
{
head->next=pre;
pre=head;
head=next;
next=next->next;
}
head->next=pre;
return head;
}
};
方法2:
递归
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* p=head->next;
ListNode* q=reverseList(p);
p->next=head;
head->next=NULL;
return q;
}
};