Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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