Concise C++, 16ms, O(n) complexity and O(1) space


  • 0
    G
        ListNode* oddEvenList(ListNode* head) {
            if (head == NULL || head->next == NULL || head->next->next == NULL) {
                return head;
            }
            ListNode* curr = head;
            ListNode* start = curr->next;
            ListNode* end = curr->next;
            while (end != NULL && end->next != NULL) {
                curr->next = end->next;
                end->next = curr->next->next;
                end = curr->next->next;
                curr->next->next = start;
                curr = curr->next;
            }
            return head;
        }
    

Log in to reply
 

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