My C++ solution using recursion


  • 0
    D

    class Solution {
    public:

    ListNode* swapPairs(ListNode* head) {
        if (!head||!head->next) return head;
        ListNode *p = head->next,*q = p->next;
        head->next->next = head;
        head->next = swapPairs(q);
        return p;
    }
    

    };


Log in to reply
 

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