/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL || pHead -> next == NULL) return pHead;
ListNode* Reverse = ReverseList(pHead -> next);
pHead -> next -> next = pHead;
pHead -> next = NULL;
return Reverse;
}
};
第二种方法:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead == NULL || pHead -> next == NULL) return pHead;
ListNode* pre = NULL;
ListNode* Reverse = NULL;
while(pHead)
{
ListNode* List = pHead -> next;
if(List == NULL) Reverse = pHead;
pHead -> next = pre;
pre = pHead;
pHead = List;
}
return Reverse;
}
};