Easy and simple c++ solution


  • 0
    S

    ...
    ListNode* swapPairs(ListNode* head) {
    ListNode* pre=nullptr;
    ListNode* temp=head;
    int pair=1;
    while(head){

            if(pair%2==0)
                swap(pre->val,head->val);
             
            
            else
            pre=head;
            
            
            head=head->next;
            pair++;
        }
        
        return temp;
        
    }
    

    ...


Log in to reply
 

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