Odd Even Linked List
Group nodes at odd positions before nodes at even positions, preserving relative order.
Open on LeetCode ↗Intuition
Read the title too fast and you will group by VALUE - odd numbers first, even numbers second. The problem means the node's POSITION, counting from 1, and the values are irrelevant. Once that is clear the shape is two chains woven through one list: odd collects positions 1, 3, 5 and even collects 2, 4, 6, each advancing by two. The bug that survives testing is forgetting to terminate the even chain. You join the odd tail to the even head at the end, but the even tail still points at whatever followed it in the original list - which is a node now living in the odd chain - so the list closes into a cycle and any traversal hangs forever. Setting the even tail's next to None is the line that makes it correct, and it is the one people leave out.
Weave two chains through the original list in a single pass, then join them. Saving even_head before the weaving starts is essential — it's the only reference to the second chain once the pointers have moved.
Approach
Read position, not value
Index from 1: the head is position 1 and therefore odd. Nothing about the stored values matters here, which is worth stating explicitly because the problem name actively misleads on this point.
Weave two pointers
Hold odd at the current odd node and even at the current even node, remembering evenHead so you can attach it later. Each iteration sets odd.next = even.next then advances odd, then even.next = odd.next then advances even. Both chains grow in place with no new nodes allocated.
Join and terminate
After the loop, odd is the last odd node and points it at evenHead. The even chain's final node must already end in None, which it does because the loop stops when even or even.next is exhausted - but only if you never re-pointed it afterwards. Getting this wrong creates a cycle rather than a wrong answer, which is why it shows up as a hang instead of a failed assertion.
Solution & live demo
Common pitfalls
Not saving the even head
odd = head even = head.next # weave, then odd.next = even
even_head = even # weave, then odd.next = even_head
By the end of the loop even points at the last even node, not the first. Linking to it creates a one-node tail and loses the rest of the even chain.
Relinking in the wrong order
even.next = odd.next odd.next = even.next
odd.next = even.next odd = odd.next even.next = odd.next even = even.next
Each assignment must read a pointer the previous one hasn't yet overwritten. Reversing them makes odd.next read the value just written, folding the list onto itself.
Getting the loop condition wrong
while even:
while even and even.next:
The body dereferences even.next to advance the odd chain. With an even-length list even.next is null on the last iteration and the loop throws.
Edge cases
Already grouped; return head unchanged.
The even chain ends naturally at the last node.
The odd chain gains the final node; even terminates before it.
Return None before touching any pointer.