9 Lines C++ 3ms solution


  • 0
    R
    class Solution {
    public:
        ListNode* swapPairs(ListNode* head) {
            if(!head|| !head->next) ;
            else{
                ListNode* temp = head->next->next;
                head->next->next = head;
                head = head->next;
                head->next->next = temp;
                head->next->next = swapPairs(head->next->next);
            }
            return head;
        }
    };
    

Log in to reply
 

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