Share my C solution


  • 0
    M
    struct ListNode* oddEvenList(struct ListNode* head) {
    if (!head || !head->next)
        return head;
    
    struct ListNode *odd, *even, *help;
    odd = head;
    even = head->next;
    help = head->next;
    
    while (odd->next != NULL && odd->next->next != NULL) {  // update odd and even lists at the same time
        odd->next = odd->next->next;
        odd = odd->next;
        
        even->next = even->next->next;
        even = even->next;
    }
    odd->next = help;
    
    return head;
    

    }


Log in to reply
 

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