双指针法:
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* temp;
ListNode* cur;
ListNode* pre;
cur=pHead;
pre=NULL;
while(cur!=NULL){
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
}
};
递归法:
class Solution {
public:
ListNode* Reverse(ListNode* cur,ListNode* pre){
if(cur==NULL) return pre;
ListNode* temp;
temp=cur->next;
cur->next=pre;
return Reverse(temp,cur);
}
ListNode* ReverseList(ListNode* pHead) {
ListNode* cur;
ListNode* pre;
return Reverse(pHead,NULL);
}
};