Odd Even Linked List – Solution & Complexity

Solution Walkthrough

1. Recognize the Pattern

The key pattern is: walk two interleaved chains (odd and even) simultaneously with a single pass, splicing each node onto its own chain as you go, then reattach the even chain after the odd chain's tail.

2. Build the Algorithm

Advance one state transition at a time. Mark or update state before exploring dependent work.

3. Check Edge Cases

Test empty or minimal input, skewed shapes, duplicates where allowed, and impossible outcomes.

4. Solution and Complexity

Time: O(n) — a single pass over the list. Space: O(1) — only a constant number of pointers are used; the existing nodes are relinked in place, not copied.

All 5 languages below run and submit against the remote judge for this problem — Java/Go/Rust use the same real ListNode structural-type support the judge added for the rest of the linked-list track.

def odd_even_list(head: ListNode) -> ListNode:
    if head is None or head.next is None:
        return head

    odd = head
    even = head.next
    even_head = even

    while even is not None and even.next is not None:
        odd.next = even.next
        odd = odd.next
        even.next = odd.next
        even = even.next

    odd.next = even_head
    return head

FAQ