Odd Even Linked List
Pattern: Traversal and dummy nodes
Split the list into two chains in one walk, the nodes at positions 1, 3, 5, … and those at 2, 4, 6, …. Then attach the even chain after the odd chain.
The problem
Given the head of a singly linked list, put all nodes at odd positions (1st, 3rd, 5th, …) first, then all nodes at even
positions, keeping the order within each group, and return the head. Use O(1) extra memory and O(n) time. For example,
1 → 2 → 3 → 4 → 5 becomes 1 → 3 → 5 → 2 → 4. Positions matter, not values.
The function name oddEvenList matches LeetCode. Use the link above for the exact statement and limits.
Think first
- Can you build two separate chains as you walk, without new nodes?
- What do you need to remember to join them at the end?
- When should the walk stop?
Hints
Brute force, and why it fails
Copy the values to two arrays and rebuild: O(n) memory. Two chains in place need O(1).
The key insight
Nodes alternate between the two chains, so each chain can grab its next node from the other chain's latest next.
One walk splits the list, and one link joins it back.
Dry run
The list is 1 → 2 → 3 → 4 → 5. odd starts on 1, even on 2, and evenHead remembers 2.
The plan
if head is null: return null
odd = head, even = head.next, evenHead = even
while even is not null and even.next is not null:
odd.next = even.next; odd = odd.next
even.next = odd.next; even = even.next
odd.next = evenHead
return head
Complexity
- Time
O(n)- Space
O(1)
One walk, and only a few pointers.
Edge cases
- Empty or one node: unchanged.
- Two nodes: unchanged.
- Even length, as in six nodes: the last even node already ends with null.
I've built my logic, show the code
Java
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null) return null;
ListNode odd = head, even = head.next, evenHead = even; // two chains: positions 1, 3, 5 … and 2, 4, 6 …
while (even != null && even.next != null) {
odd.next = even.next; // the next odd node
odd = odd.next;
even.next = odd.next; // the next even node
even = even.next;
}
odd.next = evenHead; // odd chain, then even chain
return head;
}
}C++
class Solution {
public:
ListNode* oddEvenList(ListNode* head) {
if (head == NULL) return NULL;
ListNode *odd = head, *even = head->next, *evenHead = even; // positions 1, 3, 5 … and 2, 4, 6 …
while (even != NULL && even->next != NULL) {
odd->next = even->next; // the next odd node
odd = odd->next;
even->next = odd->next; // the next even node
even = even->next;
}
odd->next = evenHead; // odd chain, then even chain
return head;
}
};Python
from lib import ListNode
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if head is None:
return None
odd, even = head, head.next # two chains: positions 1, 3, 5 … and 2, 4, 6 …
even_head = even
while even is not None and even.next is not None:
odd.next = even.next # the next odd node
odd = odd.next
even.next = odd.next # the next even node
even = even.next
odd.next = even_head # odd chain, then even chain
return headCommon mistakes
- Grouping by odd and even values instead of positions.
- Forgetting to save the first even node before rewiring.
- Stopping only on
even == null, which readsnull.nextfor odd lengths.