Problem Walkthrough

Odd Even Linked List LeetCode 328 Guide

Solve LeetCode 328 by maintaining two separate chains — one for odd-indexed nodes and one for even-indexed nodes — then connecting the odd tail to the even head for an in-place O(1) space reordering.

7 min read|

Odd Even Linked List

LeetCode 328

Problem Overview

LeetCode 328 — Odd Even Linked List — asks you to rearrange a singly linked list so that all nodes at odd index positions come first, followed by all nodes at even index positions. The first node is at index 1 (odd), the second at index 2 (even), and so on. You must return the reordered list head.

The problem is labeled Medium and is a staple in linked list interview rounds because it tests in-place pointer manipulation without extra space. The naive approach of collecting values into arrays and rewriting the list is not acceptable — the problem requires O(1) extra space.

Key constraints to keep in mind: the list can have 0 to 10,000 nodes, node values are between -1,000,000 and 1,000,000, and you must achieve O(n) time and O(1) space.

  • Input: head of singly linked list
  • Output: head of reordered list (odd indices first, even indices second)
  • Index starts at 1 — first node is odd
  • O(n) time and O(1) space required
  • List length: 0 to 10,000 nodes

The Two-Chain Strategy

The key insight is to maintain two separate pointer chains as you traverse: an odd chain collecting nodes at positions 1, 3, 5, ... and an even chain collecting nodes at positions 2, 4, 6, ... Each pointer advances two steps at a time, naturally skipping every other node.

At the end of traversal, connect the tail of the odd chain to the head of the even chain. This single pointer update joins the two groups into the correctly reordered list. The entire operation touches each node exactly once and uses only a constant number of extra pointers.

This strategy is clean because you never move or copy node values — you only redirect the next pointers. The relative order within each group (odd-indexed nodes among themselves, even-indexed nodes among themselves) is preserved.

💡

Save the Even Head First

Before starting the traversal, save a reference to the even head (head.next). You will need it at the very end to connect the odd chain's tail to the even chain's head. Once the loop runs, you can no longer recover this pointer.

Pointer Manipulation

Initialize odd to head and even to head.next. Save evenHead = even — this reference will be used at the end to connect the two chains. The loop continues while even and even.next are both non-null; when either is null all nodes have been assigned.

Inside each loop iteration, four pointer updates happen: odd.next = even.next advances odd to pick up the next odd-indexed node, then odd = odd.next advances the odd pointer forward. Then even.next = odd.next picks up the next even-indexed node, and even = even.next advances the even pointer forward.

After the loop, connect the chains with odd.next = evenHead. This appends the entire even chain after the last odd-indexed node. Return head — the original head is still the first odd-indexed node and remains the correct list head.

  1. 1Initialize: odd = head, even = head.next, evenHead = even
  2. 2Loop while even != null and even.next != null
  3. 3odd.next = even.next (odd picks up the next odd-indexed node)
  4. 4odd = odd.next (advance odd pointer)
  5. 5even.next = odd.next (even picks up the next even-indexed node)
  6. 6even = even.next (advance even pointer)
  7. 7After loop: odd.next = evenHead (connect chains)
  8. 8Return head

Why This Works

The odd and even pointers alternate through the list, each skipping one node per iteration. The odd pointer steps over even-indexed nodes to land on the next odd-indexed node; the even pointer steps over odd-indexed nodes to land on the next even-indexed node. This naturally separates the two groups without any extra data structure.

No new nodes are created and no values are copied. Only the next pointers are redirected. Because each node is visited exactly once, the algorithm runs in O(n) time. Because only a fixed number of pointer variables are used (odd, even, evenHead), the space complexity is O(1).

The loop termination condition — while even and even.next are not null — is carefully chosen. It handles both even-length and odd-length lists: for odd-length lists, the last node is odd-indexed and even will reach null after the final iteration; for even-length lists, even will reach the last node and even.next will be null.

ℹ️

Even Pointer Drives the Loop

The even pointer drives loop termination: when even or even.next is null, all nodes have been assigned to their chain. At that point odd.next still points into the even chain — that is why you must connect with the saved evenHead rather than odd.next.

Walk-Through Example

Trace through the list 1 → 2 → 3 → 4 → 5. Odd-indexed nodes are 1 (pos 1), 3 (pos 3), 5 (pos 5). Even-indexed nodes are 2 (pos 2), 4 (pos 4). After reordering the result should be 1 → 3 → 5 → 2 → 4.

Start: odd = node 1, even = node 2, evenHead = node 2. Iteration 1: odd.next = node 3, odd advances to node 3; even.next = node 4, even advances to node 4. Iteration 2: odd.next = node 5, odd advances to node 5; even.next = null (node 5 has no next), even advances to null — loop ends.

After the loop: odd = node 5, odd.next = evenHead (node 2). The odd chain is 1 → 3 → 5 and the even chain is 2 → 4 → null. After connecting: 1 → 3 → 5 → 2 → 4 → null. Return head = node 1.

  1. 1Initial: 1 → 2 → 3 → 4 → 5, odd = 1, even = 2, evenHead = 2
  2. 2Iter 1: odd.next = 3, odd = 3; even.next = 4, even = 4
  3. 3Iter 2: odd.next = 5, odd = 5; even.next = null, even = null
  4. 4Loop ends (even is null)
  5. 5Connect: odd.next = evenHead → node 5 points to node 2
  6. 6Result: 1 → 3 → 5 → 2 → 4

Code Walkthrough Python and Java

In Python: guard against an empty list or single-node list by returning head early if not head or not head.next. Set odd = head, even = head.next, evenHead = even. Enter the while loop, perform the four pointer updates each iteration, then connect odd.next = evenHead and return head. Core logic is 8 lines.

In Java: the structure is identical. Use a null check at the top, initialize the three pointers, run the while loop with the same four updates, connect the chains, and return head. The ListNode class is provided by the judge — no extra imports needed. Time O(n), space O(1).

Both solutions handle edge cases naturally: an empty list returns null via the early guard, a single-node list returns head via the early guard, and a two-node list runs zero iterations of the loop then connects odd.next = evenHead directly.

⚠️

Index Position Not Value

This problem groups nodes by index position (1st, 2nd, 3rd...) not by node value. A node with value 4 at position 1 is odd-indexed. A node with value 1 at position 2 is even-indexed. The words odd and even refer to the position, never to the stored value.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now