Browse curriculum

Odd Even Linked List

Move every node at an odd position ahead of the nodes at even positions, in place, by growing an odd chain and an even chain and joining them.

Problem Understanding

Odd Even Linked List (LeetCode 328): given the head of a singly linked list, group all nodes at odd positions together, followed by the nodes at even positions, and return the reordered list. The first node is position 1 (odd). Keep the relative order inside each group, and use O(1) extra space and O(n) time.

Examples: [1, 2, 3, 4, 5] → [1, 3, 5, 2, 4]; [2, 1, 3, 5, 6, 4, 7] → [2, 3, 6, 7, 1, 5, 4].

The Intuition: Two Chains Leapfrog

Odd and even nodes alternate, so every odd node's next odd node is two steps away, and the same for even nodes.

  1. Start two chains. The odd chain at head, the even chain at head.next. Save the start of the even chain.
  2. Leapfrog. Point the odd tail past the even tail to the next odd node, then the even tail past that to the next even node.
  3. Join. When the even chain cannot grow, hang it off the end of the odd chain.

Relinking pointers in place instead of copying values is the same skill as Reverse Linked List and Swap Nodes in Pairs.

Interactive Walkthrough

RELINK IN PLACE · O(1) SPACE

SPLIT AT HEAD

odd chain

odd

1

0

…

even chain

even

2

1

…

odd

1

even

2

evenHead

2

 

odd = head (1), even = head.next (2). Remember evenHead = 2

Next

odd.next = even.next: 1 now points past 2 to 3, the next odd node

Free preview: the first 3 of 7 steps

head

The two rows are the two chains in link order; the small number under a node is its index in the input. odd is filled and even outlined. Try Even length to see the loop stop with even on the last node.

Dry Run Table

Input: [1, 2, 3, 4, 5], evenHead = 2.

Step Change Odd chain Even chain
start odd = 1, even = 2 1 2
1 1.next = 3 1 → 3 2
2 2.next = 4 1 → 3 2 → 4
3 3.next = 5 1 → 3 → 5 2 → 4
4 4.next = NULL 1 → 3 → 5 2 → 4
join 5.next = 2 1 → 3 → 5 → 2 → 4

Answer: [1, 3, 5, 2, 4].

The Solution Template

Two tail pointers that leapfrog each other, a saved evenHead, and one join at the end.

Odd Even Linked List Code
1var oddEvenList = function(head) {
2 if (!head) {
3 return head;
4 }
5 let odd = head, even = head.next;
6 const evenHead = even;
7 while (even && even.next) {
8 odd.next = even.next;
9 odd = odd.next;
10 even.next = odd.next;
11 even = even.next;
12 }
13 odd.next = evenHead;
14 return head;
15};

Edge Cases & Common Mistakes

  • Empty list: return head (null) before touching head.next.
  • One or two nodes: the loop does not run and the list is already in order.
  • Forgetting evenHead: without it the even chain is unreachable at the join.
  • Grouping by value: the problem is about positions, not whether the values are odd.
  • Cutting the even chain off: the last even node's next must end up null, which the loop's even.next = odd.next guarantees on an odd-length list.

Summary

Keep tails for an odd chain and an even chain, leapfrog them along the list, and join the odd tail to the saved evenHead. O(n) time, O(1) space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Split

    Set odd = head, even = head.next and remember evenHead = even.

  2. Relink

    While even and even.next exist: odd.next = even.next, advance odd; even.next = odd.next, advance even.

  3. Join

    Set odd.next = evenHead and return head.

Frequently asked questions

Does "odd" mean odd values or odd positions?

Positions. The first node is odd, the second even, and so on, whatever values they hold. In [2, 1, 3, 5, 6, 4, 7] the odd-position nodes are 2, 3, 6, 7.


Why save evenHead before the loop?

The loop moves even along the even chain, so by the end nothing points at its first node except head.next, which the loop has overwritten. Saving it up front is the only way to join the chains afterwards.


Why is the loop condition even and even.next?

Each round needs the node after even to become the next odd node. If even is null or is the last node, there is no further odd node and both chains are complete.


How is this O(1) space?

No new nodes or arrays are created. Only the next pointers of existing nodes change, using a fixed number of pointer variables: odd, even and evenHead.

New to the technique behind this problem? Watch it run step by step:

Singly Linked List Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description