示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
/**
* 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) return NULL;
if(!head->next) {head->next=NULL; return head;}
ListNode* pt=head;
while(pt->next->next)
{
pt=pt->next;
}
ListNode* t=pt->next;
pt->next=NULL;
t->next=reverseList(head);
return t;
}
};
/**
* 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) return NULL;
ListNode* prev=NULL;
ListNode* curr=head;
while(curr!=NULL)
{
ListNode* tmp=curr->next;
curr->next=prev;
prev=curr;
curr=tmp;
}
return prev;
}
};