Very simple C++ recursive solution.

  • 8
    class Solution {
        ListNode* swapPairs(ListNode* head) {
            if (head == NULL || head->next == NULL) return head;
            ListNode *grandChild = swapPairs(head->next->next);
            ListNode *child = head->next;
            child->next = head;
            head->next = grandChild;
            return child;

  • 0

    This will not be using constant space because of the recursive function calls.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.