/**
* 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* pre=head;
ListNode* now=head->next;
ListNode* newHead=NULL;
while(pre && now)
{
ListNode* temp=now->next;
pre->next=newHead;
now->next=pre;
newHead=now;
if(temp==NULL)
return newHead;
else
{
pre=temp;
now=temp->next;
}
}
pre->next=newHead;
return pre;
}
};
Reverse Linked List
最新推荐文章于 2020-10-07 22:19:06 发布