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) {
if(head == NULL || head->next == NULL){
return head;
}
ListNode *p = head->next;
head->next = swapPairs(head->next->next);
p->next = head;
return p;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment