Odd Even Linked List
Odd Even Linked List is LeetCode 328 (Medium). You get the head of a singly linked list. Reorder it so that every node in an odd position (1st, 3rd, 5th, …) comes first, followed by every node in an even position, and return the head.
- Positions, not values. The first node counts as odd even when it holds the number 2.
- Inside each group the nodes keep their original order.
- The task asks for O(n) time and O(1) extra space, so the existing nodes must be relinked. Copying values into new nodes or an array is not allowed.
- The number of nodes in the linked list is in the range [0, 10⁴].
- -10⁶ <= Node.val <= 10⁶
Intuition
Read the list as two chains woven together: odd positions and even positions take turns. Unweave them in one pass, then attach the even chain to the end of the odd chain.
- Two tails:
oddis the last node of the odd chain andeventhe last node of the even chain. The node right afterevenis always the next odd node, and the one after that is the next even node, so each round both tails hop over one node of the other chain. - Save the first even node: once the weaving starts, nothing in the list points to node 2 any more. A saved reference is the only way to find the even chain for the final join.
"Group nodes by position" or "split a list into two lists and glue them" with O(1) space means relinking pointers in place with one tail per group.
Approach
Before reading on, draw five nodes and two pointers, one on node 1 and one on node 2. Which pointer changes first, and what must you save so the second group is not lost? Aim for O(n) time and O(1) space.
Return an empty list as it is
If head is null there is nothing to reorder. Checking this first matters because the next step reads head.next.
Start both chains
Set odd = head, even = head.next, and keep even_head = even. Node 1 begins the odd chain and node 2 begins the even chain, and even_head remembers where the even chain starts.
Link the next odd node
Set odd.next = even.next and move odd there. The node after the even tail sits one position later than an even position, so it is odd and belongs on the odd chain.
Link the next even node
Set even.next = odd.next and move even there. The node after the new odd tail is the next even one. On the last round this copies a null, which is what ends the even chain cleanly.
Stop when the even tail has nothing after it
Loop while even and even.next both exist. If either is missing, no odd node is left to collect: an odd-length list ends with even at null, an even-length list ends with even on the last node.
Join the two chains
Set odd.next = even_head and return head. The odd tail is the last odd node, so pointing it at the first even node puts the whole even chain after all the odd ones.
Code in Python, C++ and Java
Common pitfalls
Joining to head.next instead of the saved node
odd.next = head.next
odd.next = even_head
By the end of the loop head.next has been rewired to node 3. For [1, 2, 3] the odd tail is node 3, so the line points node 3 at itself and the list becomes a loop. Node 2 is only reachable through the reference saved before the loop.
Checking only even.next in the loop
while even.next:
while even and even.next:
On an odd-length list the even tail becomes null after the last round. For [1, 2, 3] the first round sets even to null, and the next check reads .next of null, which crashes.
Edge cases
Returned at once. Without that check, head.next would be read on a null head.
The loop body never runs. The join sets node 1's next to node 2 (or to null for one node), which is the order the list already had.
Complexity
Odd Even Linked List FAQ
Can I collect the nodes in two arrays and relink them?
It gives the right order, but the arrays hold n references, which is O(n) extra space. The task asks for O(1), and the two-tail loop does the same job without storing anything.
How is this different from Partition List?
Partition List splits nodes by comparing each value with x, so either group can get the next node and two dummy heads are needed. Here the groups strictly alternate, so the first two nodes start the chains and no dummies are needed.