/**
* 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
最新推荐文章于 2024-11-04 16:11:14 发布