单链表翻转(递归和迭代两种)
迭代
/**
* 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 Result(0);
ListNode *p=head;
while(p)
{
ListNode *q=p->next;
p->next=Result.next;
Result.next=p;
p=q;
}
return Result.next;
}
};
递归
/**
* 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) return NULL;
if(head->next==NULL) return head;
ListNode *p=head->next;
ListNode *n=reverseList(p);
head->next=NULL;
p->next=head;
return n;
}
};