Odd Even Linked List
Medium
Topics
Given the head of a singly linked list, group all the nodes at odd positions together followed by the nodes at even positions (1-indexed), preserving relative order within each group. Return the reordered list.
Example 1
Input: head = [1,2,3,4,5] Output: [1,3,5,2,4]
Example 2
Input: head = [2,1,3,5,6,4,7] Output: [2,3,6,7,1,5,4]
Constraints
- 0 <= number of nodes <= 10^4
- -10^6 <= Node.val <= 10^6
Editorial — intuition, approach, complexity and pitfalls
Intuition
The two output groups are already interleaved in the input, so nothing needs to be built — the list is unzipped into an odd chain and an even chain, and then the even chain is stapled onto the end of the odd one. No new nodes, no values moved.
Two pointers walk in lockstep, each advancing two positions per iteration by hopping over the other's next node. Hold the head of the even chain before the loop starts, because that is the only reference to it once the relinking begins.
Approach
- Return immediately for an empty or single-node list.
- Point
oddat the head andevenat the second node; saveeven_head. - While
evenandeven.nextboth exist, spliceoddtoeven.nextand advance, then spliceevento the newodd.nextand advance. - Attach
even_headafter the final odd node.
Dry Run
input 1 -> 2 -> 3 -> 4 -> 5
odd chain 1 -> 3 -> 5
even chain 2 -> 4
join 1 -> 3 -> 5 -> 2 -> 4
a longer check, verified against the shown code
[2,1,3,5,6,4,7] -> 2 -> 3 -> 6 -> 7 -> 1 -> 5 -> 4
[1,2] -> 1 -> 2 (loop body never runs)Complexity
- Time — O(n): each node is visited once by one of the two pointers.
- Space — O(1): three pointers. Positions are tracked implicitly by the lockstep walk, so no counter is needed.
Common Pitfalls
- Swapping values rather than relinking pointers. It produces the same output here, but the problem is about pointer surgery, and the trick breaks the moment nodes carry anything beyond a comparable value.
- Weakening
while even and even.next. Both tests are load-bearing: without the second, an even-length list dereferences null at the last step; without the first, an odd-length one does. - Losing
even_head. Onceodd.nextis reassigned the original second node is unreachable, so it must be saved before the loop. - Joining the chains inside the loop instead of after it, which closes a cycle and makes the next iteration walk forever.
- Grouping by node value parity. The problem groups by position.
- Forgetting that the final odd node's
nextmust be overwritten, not appended to — it still points into the even chain mid-list until the join happens.
Full Solution — all languages
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def oddEvenList(self, head):
if not head or not head.next:
return head
odd = head
even = head.next
even_head = even
while even and even.next:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = even_head
return headimport java.util.*;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
class Solution {
public ListNode oddEvenList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode odd = head, even = head.next, evenHead = even;
while (even != null && even.next != null) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
}
}#include <bits/stdc++.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x): val(x), next(nullptr) {
}
};
class Solution {
public: ListNode* oddEvenList(ListNode* head) {
if (!head || !head->next) return head;
ListNode* odd = head;
ListNode* even = head->next;
ListNode* evenHead = even;
while (even && even->next) {
odd->next = even->next;
odd = odd->next;
even->next = odd->next;
even = even->next;
}
odd->next = evenHead;
return head;
}
};/** Node shape: { val, next }. Build/return plain objects or use this ctor. */ function ListNode(val, next) {
this.val = (val === undefined ? 0 : val);
this.next = (next === undefined ? null : next);
}
/**
* @param {ListNode} head
* @return {ListNode}
*/ var oddEvenList = function(head) {
if (!head || !head.next) return head;
let odd = head, even = head.next;
const evenHead = even;
while (even && even.next) {
odd.next = even.next;
odd = odd.next;
even.next = odd.next;
even = even.next;
}
odd.next = evenHead;
return head;
};