Simple accepted C++ solution


  • 0
    P
    ListNode* oddEvenList(ListNode* head) {
        if(!head || !head->next || !head->next->next)
            return head;        
    
        ListNode* temp=head;
        ListNode* prevDelete;
        prevDelete=temp->next;
    
        while(prevDelete && prevDelete->next){
                ListNode* nodeInserted=prevDelete->next;
                prevDelete->next=prevDelete->next->next;
                prevDelete=prevDelete->next;
                
                ListNode* save=temp->next;
                temp->next=nodeInserted;
                temp->next->next=save;
                temp=temp->next;      
        }
        return head;
    }
    

    Logic: keep a pointer to the nodes to be deleted, keep another pointer at a place we need to add them. We stop the loop when the first pointer reaches NULL.


Log in to reply
 

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