My simple recursive C++ solution


  • 0
    C
    ListNode* swapPairs(ListNode* head)
    {
        if(!head) return nullptr;
        ListNode *frontPtr=head,*backPtr=head->next;
        while(frontPtr && backPtr)
        {
            swap(frontPtr->val,backPtr->val);
    
            frontPtr=frontPtr->next;
            if(frontPtr!=nullptr)
                frontPtr=frontPtr->next;
    
            backPtr=backPtr->next;
            if(backPtr!=nullptr)
                backPtr=backPtr->next;
        }
        return head;
    }

  • 0
    S

    This is not recursive.


Log in to reply
 

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