LeetCode #328 Medium

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.
Constraints
  • The number of nodes in the linked list is in the range [0, 10⁴].
  • -10⁶ <= Node.val <= 10⁶
linked-listtwo-pointers
Open on LeetCode ↗
02

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: odd is the last node of the odd chain and even the last node of the even chain. The node right after even is 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.
How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

5

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.

6

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
▶3 if not head:
▶4 return head
▶5 odd, even = head, head.next
▶6 even_head = even
▶7 while even and even.next:
▶8 odd.next = even.next
▶9 odd = odd.next
▶10 even.next = odd.next
▶11 even = even.next
▶12 odd.next = even_head
▶13 return head
positionoddeven1234512345oddevensavedodd tail on 1, even tail on 2 (saved)
odd1
even2
even_head2
Position 1 starts the odd chain and position 2 starts the even chain. Node 2 is saved now: once the relinking starts, no node will point to it any more, and the final join needs it.
positionoddeven1234512345oddevensaved2 has a next node → keep going
even2
even.next3
The even tail still has a node after it, at position 3. That node is odd, so there is more to collect.
positionoddeven1234512345oddevensavedlink 1 to 3, the next odd node
odd3
even2
Position 3 comes right after the even tail, so it is the next odd node. 1 now skips over 2 and points to it, and the odd tail moves there.
positionoddeven1234512345oddevensavedlink 2 to 4, the next even node
odd3
even4
Position 4 follows the new odd tail, so it is the next even node. 2 now skips over 3 and points to it.
positionoddeven1234512345oddevensaved4 has a next node → keep going
even4
even.next5
The even tail still has a node after it, at position 5. That node is odd, so there is more to collect.
positionoddeven1234512345oddevensavedlink 3 to 5, the next odd node
odd5
even4
Position 5 comes right after the even tail, so it is the next odd node. 3 now skips over 4 and points to it, and the odd tail moves there.
positionoddeven1234512345oddeven: nonesaved4 gets no next → even tail is none
odd5
evennone
Nothing follows 5, so 4 copies that empty link: the even chain now ends at 4, and the even tail moves off the end of the list.
positionoddeven1234512345oddeven: nonesavedeven tail is none → stop
evennone
The list had an odd length, so the last node went to the odd chain and the even tail fell off the end. No odd node is left.
positionoddeven1234512345oddeven: nonesavedlink last odd 5 to saved 2
odd.next2
The odd tail 5 is the last odd node. Pointing it at the saved first even node hangs the whole even chain after the odd one. The even chain already ends in nothing, so there is no loop.
positionoddeven1234512345savedresult: 1 → 3 → 5 → 2 → 4
return1
Following the arrows from the first node now visits every odd position, then every even position, each group in its original order. The head never changed, so the original head is returned.
05

Common pitfalls

Joining to head.next instead of the saved node

✗ Wrong
odd.next = head.next
✓ Right
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

✗ Wrong
while even.next:
✓ Right
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.

06

Edge cases

Empty list

Returned at once. Without that check, head.next would be read on a null head.

One or two nodes

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.

07

Complexity

Time
O(n)
Space
O(1)
Each round moves both tails forward by two positions in the original list, so every node is visited once. Only three pointers are kept, whatever the length.
08

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.