Odd Even Linked List (LeetCode 328): Solution and Explanation

Medium
Linked List

Given the head of a singly linked list, reorder it so that all nodes at odd positions (first, third, fifth and so on) come first, followed by all nodes at even positions, keeping the relative order inside each group. Positions are what matter, not the values, and the follow-up asks for O(1) extra space and O(n) time.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

If memory were free, you could copy the nodes into an array, then rebuild the list by linking indices 0, 2, 4 and so on followed by 1, 3, 5. That works in linear time but uses O(n) space, which the problem rules out.

Picture the list as two interleaved chains. The odd-position nodes are 1, 3, 5, and the even-position nodes are 2, 4, 6, and each node's next currently points into the other chain. To separate them, every node simply needs to point two steps ahead instead of one: node 1 should point to node 3, node 2 to node 4, and so on. You can do this with two pointers, odd and even, walking in tandem. Each pointer reaches over its partner to grab the next node of its own chain.

After the walk, you have two independent chains, and the last step is to attach the head of the even chain to the tail of the odd chain. That requires remembering where the even chain started, because by the end even has moved past it, so save even_head before the loop. Since the walk rewires pointers in place and each node is touched once, this meets both the time and space requirements.

Approach: Weave two chains in place

  1. If the list is empty, return None.
  2. Set odd = head, even = head.next, and save even_head = even so the even chain can be attached later.
  3. While even and even.next exist, link odd.next = even.next and advance odd to that node.
  4. Then link even.next = odd.next and advance even to that node.
  5. When the loop ends, odd is the last odd-position node, so set odd.next = even_head.
  6. Return head, which is still the first odd node.

Step-by-step example

head = [1, 2, 3, 4, 5]
  1. Start with odd on 1, even on 2, and even_head on 2.
  2. First iteration: even.next is 3, so 1.next = 3 and odd moves to 3. Then 2.next = 4 and even moves to 4.
  3. Second iteration: even.next is 5, so 3.next = 5 and odd moves to 5. Then 4.next = 5.next, which is None, and even becomes None.
  4. The loop stops because even is None. The odd chain is 1, 3, 5 and the even chain is 2, 4.
  5. Setting 5.next = even_head joins them, and the function returns 1, 3, 5, 2, 4.

Code: Python, Java, C++

1class Solution:
2    def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3        if not head:
4            return None
5
6        odd = head
7        even = head.next
8        even_head = even
9
10        while even and even.next:
11            odd.next = even.next
12            odd = odd.next
13            even.next = odd.next
14            even = even.next
15
16        odd.next = even_head
17        return head

Time and space complexity

Time

O(n)

Each iteration advances both pointers by one node in their own chains, so every node is visited once.

Space

O(1)

Only three pointers are used and the nodes are relinked in place.

Other approaches

Two dummy heads (time O(n), space O(1))

Walk the list once with a position counter, appending each node to either an odd dummy chain or an even dummy chain, then join the chains and terminate the even one. It is slightly longer but very explicit and generalizes to splitting by any rule.

Array of nodes (time O(n), space O(n))

Collect node references into an array, then relink them in the order of even indices followed by odd indices. It is easy to get right, but uses O(n) extra memory, which the follow-up forbids.

Common mistakes

  • Grouping by value instead of position. Odd and even refer to the index of the node in the list, not whether node.val is odd.
  • Not saving even_head before the loop. By the end even points at the tail or None, and the start of the even chain is lost.
  • Checking only even.next in the loop condition. On odd-length lists even itself becomes None at the end, and reading even.next then raises an error.
  • In the dummy-head variant, forgetting to set the even chain's tail to None, which leaves a cycle when the last node was odd.

Frequently asked questions

Does odd and even mean the node value or its position?

Its position. The first node is odd, the second is even, and so on, regardless of what values the nodes hold.

Why is the loop condition even and even.next?

even is always one step ahead of odd, so it is the first to reach the end. If even is None or has no next node, there is no further odd node to link, so the weaving is finished.

Can Odd Even Linked List be solved in O(1) space?

Yes. The two-pointer weave rewires the existing nodes in place and only stores a few pointers, so no extra memory grows with the list.