Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created November 17, 2018 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/0438abb290ca2a8fc2c6cb072f8c575d to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/0438abb290ca2a8fc2c6cb072f8c575d to your computer and use it in GitHub Desktop.
/**
* 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) {
ListNode* dummy = new ListNode(0);
dummy->next = head;
ListNode* p = dummy;
while(p->next&&p->next->next){
ListNode* p1 = p->next;
ListNode* p2 = p->next->next;
p->next = p2;
p1->next = p2->next;
p2->next = p1;
p = p1;
}
return dummy->next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment