c++ solution beats 100% of submission


  • 0
    T
        ListNode* swapPairs(ListNode* head) {
            
            if (NULL == head)
                return NULL;
            
            if (NULL == head->next)
                return head;
            
            ListNode* even = head;
            ListNode* odd = head->next;
            even->next = odd->next;
            odd->next = even;
            ListNode* newHead = odd;
    
            while (even->next && odd->next && even->next->next && odd->next->next)
            {
                ListNode* temp = even;
                even = even->next;
                odd = even->next;
                even->next = odd->next;
                odd->next = even;
                temp->next = odd;
            }
            return newHead;   
        }
    };
    

Log in to reply
 

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