My simple c++ solution


  • 0
    K
    class Solution {
    public:
    ListNode* swapPairs(ListNode* head) {
        
        ListNode * prev=head;
        if(!head||!head->next)return head;
        ListNode * first=head, *second=head;
        while(first && first->next){
            second=first->next;
            first->next=second->next;
            second->next=first;
            if(first!=head)prev->next=second;
            else head=second;
            prev=first;
            first=first->next;
        }
        return head;
    }
    

    };


Log in to reply
 

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