/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode *dummy=new ListNode(0);
dummy->next=head;
ListNode *cur=dummy;
while(cur->next!=NULL&&cur->next->next!=NULL)
{
ListNode *tmp=cur->next;
cur->next=tmp->next;
tmp->next=tmp->next->next;
cur->next->next=tmp;
cur=tmp;
}
return dummy->next;
}
};
【leetcode】Swap Nodes in Pairs
最新推荐文章于 2024-10-10 01:02:39 发布