标签:
Problem:
Reverse a singly linked list.
recursion:
/**
* 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* p = head->next;
ListNode* n = reverseList(p);
head->next = NULL;
p->next = head;
return n;
}
};
标签:
原文地址:http://www.cnblogs.com/liutianyi10/p/5562849.html