Odd Even Linked List (LeetCode 328): Solution and Explanation
Given the head of a singly linked list, reorder it so that all nodes at odd positions (first, third, fifth and so on) come first, followed by all nodes at even positions, keeping the relative order inside each group. Positions are what matter, not the values, and the follow-up asks for O(1) extra space and O(n) time.
Watch it run step by step
Try your own input and write the code →Intuition
If memory were free, you could copy the nodes into an array, then rebuild the list by linking indices 0, 2, 4 and so on followed by 1, 3, 5. That works in linear time but uses O(n) space, which the problem rules out.
Picture the list as two interleaved chains. The odd-position nodes are 1, 3, 5, and the even-position nodes are 2, 4, 6, and each node's next currently points into the other chain. To separate them, every node simply needs to point two steps ahead instead of one: node 1 should point to node 3, node 2 to node 4, and so on. You can do this with two pointers, odd and even, walking in tandem. Each pointer reaches over its partner to grab the next node of its own chain.
After the walk, you have two independent chains, and the last step is to attach the head of the even chain to the tail of the odd chain. That requires remembering where the even chain started, because by the end even has moved past it, so save even_head before the loop. Since the walk rewires pointers in place and each node is touched once, this meets both the time and space requirements.
Approach: Weave two chains in place
- If the list is empty, return None.
- Set
odd = head,even = head.next, and saveeven_head = evenso the even chain can be attached later. - While
evenandeven.nextexist, linkodd.next = even.nextand advanceoddto that node. - Then link
even.next = odd.nextand advanceevento that node. - When the loop ends,
oddis the last odd-position node, so setodd.next = even_head. - Return
head, which is still the first odd node.
Step-by-step example
head = [1, 2, 3, 4, 5]
- Start with
oddon 1,evenon 2, andeven_headon 2. - First iteration:
even.nextis 3, so1.next = 3andoddmoves to 3. Then2.next = 4andevenmoves to 4. - Second iteration:
even.nextis 5, so3.next = 5andoddmoves to 5. Then4.next = 5.next, which is None, andevenbecomes None. - The loop stops because
evenis None. The odd chain is 1, 3, 5 and the even chain is 2, 4. - Setting
5.next = even_headjoins them, and the function returns 1, 3, 5, 2, 4.
Code: Python, Java, C++
1class Solution:
2 def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
3 if not head:
4 return None
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 even.next = odd.next
14 even = even.next
15
16 odd.next = even_head
17 return head1class Solution {
2 public ListNode oddEvenList(ListNode head) {
3 if (head == null) return null;
4
5 ListNode odd = head;
6 ListNode even = head.next;
7 ListNode evenHead = even;
8
9 while (even != null && even.next != null) {
10 odd.next = even.next;
11 odd = odd.next;
12 even.next = odd.next;
13 even = even.next;
14 }
15
16 odd.next = evenHead;
17 return head;
18 }
19}1class Solution {
2public:
3 ListNode* oddEvenList(ListNode* head) {
4 if (!head) return nullptr;
5
6 ListNode* odd = head;
7 ListNode* even = head->next;
8 ListNode* evenHead = even;
9
10 while (even && even->next) {
11 odd->next = even->next;
12 odd = odd->next;
13 even->next = odd->next;
14 even = even->next;
15 }
16
17 odd->next = evenHead;
18 return head;
19 }
20};Time and space complexity
O(n)
Each iteration advances both pointers by one node in their own chains, so every node is visited once.
O(1)
Only three pointers are used and the nodes are relinked in place.
Other approaches
Two dummy heads (time O(n), space O(1))
Walk the list once with a position counter, appending each node to either an odd dummy chain or an even dummy chain, then join the chains and terminate the even one. It is slightly longer but very explicit and generalizes to splitting by any rule.
Array of nodes (time O(n), space O(n))
Collect node references into an array, then relink them in the order of even indices followed by odd indices. It is easy to get right, but uses O(n) extra memory, which the follow-up forbids.
Common mistakes
- Grouping by value instead of position. Odd and even refer to the index of the node in the list, not whether
node.valis odd. - Not saving
even_headbefore the loop. By the endevenpoints at the tail or None, and the start of the even chain is lost. - Checking only
even.nextin the loop condition. On odd-length listsevenitself becomes None at the end, and readingeven.nextthen raises an error. - In the dummy-head variant, forgetting to set the even chain's tail to None, which leaves a cycle when the last node was odd.
Frequently asked questions
Does odd and even mean the node value or its position?
Its position. The first node is odd, the second is even, and so on, regardless of what values the nodes hold.
Why is the loop condition even and even.next?
even is always one step ahead of odd, so it is the first to reach the end. If even is None or has no next node, there is no further odd node to link, so the weaving is finished.
Can Odd Even Linked List be solved in O(1) space?
Yes. The two-pointer weave rewires the existing nodes in place and only stores a few pointers, so no extra memory grows with the list.