/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head)
{
if(head==NULL || head->next==NULL)
return head;
ListNode* node1=head;
ListNode* node2=head->next;
ListNode* pre=NULL;
ListNode* newHead=node2;
while(node2)
{
ListNode* temp=node2->next;
node2->next=node1;
node1->next=temp;
if(pre)
pre->next=node2;
pre=node1;
if(temp!=NULL)
{
node1=temp;
node2=temp->next;
}
else
break;
}
return newHead;
}
};
Swap Nodes in Pairs
最新推荐文章于 2019-05-11 21:02:45 发布