C++ O(1) solution


  • -1
    J
    ListNode* oddEvenList(ListNode* head) {
        if(!head) return NULL;
        
        ListNode* evenHead = head->next;
        ListNode* odd = head;
        ListNode* even = evenHead;
        
        while(even && even->next) {
            ListNode* t1 = odd->next->next;
            ListNode* t2 = even->next->next;
            odd->next = t1; even->next = t2;
            odd = odd->next; even = even->next;
        }
        
        odd->next = evenHead;
        return head;
    }

Log in to reply
 

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