Odd Even Linked List
Problem
Given the head of a singly linked list, reorder the list so that all nodes at odd indices are grouped together followed by the nodes at even indices, preserving the relative order within each group, and return the reordered list's head.
- The number of nodes in the linked list is in the range [0, 10⁴]
- −10⁶ ≤ Node.val ≤ 10⁶
Example
head = [1,2,3,4,5][1,3,5,2,4]Starting with the list 1->2->3->4->5, the algorithm separates nodes into odd and even indexed groups. The odd nodes are 1, 3, 5 and the even nodes are 2, 4. The algorithm links all odd nodes in order, then appends the even nodes. The final reordered list is 1->3->5->2->4.
Approach
Straightforward Solution
A naive approach would be to traverse the list, collect nodes into two separate arrays or lists (odd and even), then reconstruct the linked list by concatenating these arrays. This requires O(n) extra space and two passes.
Core Observation
The problem requires rearranging nodes based on their position indices, not their values. The key insight is that odd and even nodes form two separate linked lists that must be merged at the end.
Path to Optimal
The optimal approach uses two pointers to track the current odd and even nodes during a single traversal. By rewiring the next pointers in-place, the algorithm partitions the list into odd and even sublists without extra space. After traversal, the odd list's tail is connected to the head of the even list, preserving relative order and achieving O(1) space and O(n) time.
Optimal Approach
Initialize two pointers: one for odd nodes starting at head, and one for even nodes starting at head.next. Keep a reference to the even head. Iterate while even and even.next exist, rewiring odd.next to even.next and moving odd forward, then rewiring even.next to odd.next and moving even forward. Finally, link the last odd node to the even head. Return the original head as the reordered list.
Time
O(n)
The algorithm traverses the linked list once, performing constant-time pointer updates per node.
Space
O(1)
The solution uses only a fixed number of pointers regardless of input size, performing all operations in-place without additional data structures.
Pattern Spotlight
Linked Lists (In-Place Reordering)
When reordering linked lists by position or grouping, maintain separate pointers for each group and rewire next pointers in a single pass to achieve O(1) space without losing track of sublist heads.
Solution
| 1 | class Solution: |
| 2 | def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: |
| 3 | if not head or not head.next: |
| 4 | return head |
| 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 | |
| 14 | even.next = odd.next |
| 15 | even = even.next |
| 16 | |
| 17 | odd.next = even_head |
| 18 | |
| 19 | return head |
Step-by-Step Solution
Handle Base Cases with Empty or Single-Node Lists
| 3 | if not head or not head.next: |
| 4 | return head |
Objective
To immediately return the input list if it is empty or contains only one node, as no reordering is needed.
Key Insight
Lists with zero or one node are already ordered by definition. Early returning avoids unnecessary processing and prevents null pointer exceptions in subsequent logic.
Interview Quick-Check
State & Boundaries
Check if head is None or head.next is None to handle trivial cases upfront.
Common Pitfalls & Bugs
Failing to handle empty or single-node lists can cause runtime errors or incorrect results.
Initialize Pointers to Track Odd and Even Sublists
| 6 | odd = head |
| 7 | even = head.next |
| 8 | even_head = even |
Objective
To set up pointers for the current odd node, current even node, and the head of the even sublist for later concatenation.
Key Insight
Separating the list into two sublists requires tracking the start of the even nodes to reconnect after rearrangement. The odd pointer starts at the head, and the even pointer starts at the second node. This setup enables in-place partitioning in a single pass.
Interview Quick-Check
Core Logic
Initialize odd to head, even to head.next, and store even_head to reconnect later.
State & Boundaries
Pointers must be initialized before entering the loop to maintain correct traversal and linkage.
Iteratively Rewire Next Pointers to Separate Odd and Even Nodes
| 10 | while even and even.next: |
| 11 | odd.next = even.next |
| 12 | odd = odd.next |
| 14 | even.next = odd.next |
| 15 | even = even.next |
Objective
To traverse the list, rewiring odd nodes to skip even nodes and even nodes to skip odd nodes, effectively partitioning the list into two linked lists.
Key Insight
By moving odd.next to even.next and then advancing odd, the algorithm collects all odd-indexed nodes. Similarly, moving even.next to odd.next and advancing even collects even-indexed nodes. The loop condition ensures traversal stops when no further even nodes exist, preserving list integrity.
Interview Quick-Check
Core Logic
The loop rewires odd.next and even.next pointers to partition nodes by index parity in a single pass.
State & Boundaries
Loop continues while even and even.next are not None to avoid null pointer dereferences.
Common Pitfalls & Bugs
Incorrect pointer updates or loop conditions can cause cycles or lost nodes.
Concatenate Odd and Even Sublists to Form the Final Reordered List
| 17 | odd.next = even_head |
Objective
To link the last odd node to the head of the even sublist, completing the reordering.
Key Insight
After partitioning, the odd sublist ends at the last odd-indexed node. Connecting odd.next to even_head appends the even sublist, preserving the relative order within each group and producing the final reordered list.
Interview Quick-Check
Core Logic
Linking odd.next to even_head merges the two sublists into the required odd-even order.
State & Boundaries
This step must occur after the loop to ensure all nodes are correctly partitioned.
Return the Head of the Reordered List
| 19 | return head |
Objective
To return the head pointer of the reordered linked list as the function's result.
Key Insight
The head of the list remains unchanged since the first node is always odd-indexed. Returning head provides access to the fully reordered list.
Interview Quick-Check
Core Logic
Return the original head as the entry point to the reordered list.
Line Analysis
This solution has 5 Critical lines interviewers watch for.
odd.next = even.next
Rewire odd.next to skip the current even node and point to the next odd node.
This step partitions the list by linking odd nodes together, effectively removing even nodes from the odd sublist.
even.next = odd.next
Rewire even.next to skip the current odd node and point to the next even node.
This step partitions the list by linking even nodes together, effectively removing odd nodes from the even sublist.
if not head or not head.next:
Check if the list is empty or has only one node.
This early return handles trivial cases where no reordering is needed and prevents null pointer errors in subsequent logic.
even_head = even
Store the head of the even sublist for later concatenation.
Preserving the even head is essential to append the even nodes after the odd nodes once partitioning is complete.
odd.next = even_head
Link the last odd node to the head of the even sublist.
This concatenation step merges the two partitions into the final reordered list, preserving relative order within each group.
return head
Return the head immediately for empty or single-node lists.
Returning early avoids unnecessary computation and maintains correctness for minimal input sizes.
while even and even.next:
Begin loop to traverse while even and even.next exist.
The loop condition ensures safe traversal and pointer updates without dereferencing null pointers.
return head
Return the head of the reordered list.
The head remains the first node, providing access to the fully reordered list after partitioning.
odd = head
Initialize pointer to the first odd-indexed node (head).
This pointer tracks the current node in the odd sublist and is used to rewire odd nodes during traversal.
even = head.next
Initialize pointer to the first even-indexed node (head.next).
This pointer tracks the current node in the even sublist and is used to rewire even nodes during traversal.
odd = odd.next
Advance the odd pointer to the next odd node.
Moving odd forward maintains the traversal and prepares for the next rewiring step.
even = even.next
Advance the even pointer to the next even node.
Moving even forward maintains traversal and prepares for the next rewiring step.
Test Your Understanding
Why must the algorithm keep a separate reference to the head of the even nodes?
Because the even nodes are disconnected from the original list during reordering, keeping a reference to the even head allows the algorithm to append the even sublist to the end of the odd sublist after traversal.
Related Problems
Linked Lists pattern
Don't just read it. Drill it.
Reconstruct Odd Even Linked List from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Odd Even Linked List drill