Community Problem

Odd Even Linked List

Difficulty: Medium

Reorder a singly-linked list so all odd-positioned nodes come before even-positioned ones, in O(n) time and O(1) space using two interleaved sub-lists.

Odd Even Linked List

Reorder a singly-linked list so all odd-positioned nodes come before even-positioned ones, in O(n) time and O(1) space using two interleaved sub-lists.

MEDIUM
Free
linked-list
two-pointers
nehawood

By @nehawood

March 27, 2026

·

Updated May 18, 2026

512 views

3

4.5 (13)

I keep this one in my linked-list rotation because the obvious approach (build two new lists with new nodes) misses the constraint: in-place pointer rewiring with no new allocations. The clean version uses just four pointers (oddHead, oddTail, evenHead, evenTail) and a single pass.

Odd Even Linked List

Given the head of a singly-linked list, group all nodes at odd positions (the 1st, 3rd, 5th, ...) followed by all nodes at even positions (the 2nd, 4th, 6th, ...) and return the reordered head. Position is by index in the input list, not by node value. Relative order within each group must be preserved.

The reordering must be done in O(n) time and O(1) extra space (i.e. by re-pointing existing nodes, not by allocating new ones).

Examples

Example 1:

  • Input: head = [1, 2, 3, 4, 5]
  • Output: [1, 3, 5, 2, 4]
  • Explanation: Odd positions (1st, 3rd, 5th) hold values 1, 3, 5. Even positions (2nd, 4th) hold values 2, 4. Concatenate.

Example 2:

  • Input: head = [2, 1, 3, 5, 6, 4, 7]
  • Output: [2, 3, 6, 7, 1, 5, 4]
  • Explanation: Odd positions hold 2, 3, 6, 7. Even positions hold 1, 5, 4.

Example 3:

  • Input: head = [1]
  • Output: [1]
  • Explanation: Single node.

Example 4:

  • Input: head = []
  • Output: []
  • Explanation: Empty list.

Constraints

  • The number of nodes in the list is in the range [0, 10^4].
  • -10^6 <= Node.val <= 10^6

Solution

Hints

0/4
Hint 1
Hint 2
Hint 3
Hint 4
All Problems