class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head;
ListNode * p = head->next;
head->next = NULL;
while(NULL!=p)
{
ListNode *tmp = p->next;
p->next = head;
head = p;
p = tmp;
}
return head;
}
};
递归方法:
pNode ReverseListRecursivly(pNode pPre,pNode pCur)
{
if(pCur == NULL)
return NULL;
if(pCur->next == NULL)
{
pCur->next = pPre;
return pCur;
}
pNode pNext = pCur->next;
pCur->next = pPre;
pNode pNewHead = ReverseListRecursivly(pCur,pNext);
return pNewHead;
}
pNode ReverseList2(pNode pHead)
{
return ReverseListRecursivly(NULL,pHead);
}