Odd Even Linked List

Medium

Topics
Linked List

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

before12345null
after13524null
odd positions (1st, 3rd, …)
Grouping is by position, not value.
Linked list values from head to tail.
Input:  head = [1,2,3,4,5]
Output: [1,3,5,2,4]

Example 2

before2135647null
after2367154null
odd positions (1st, 3rd, …)
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

  1. Return immediately for an empty or single-node list.
  2. Point odd at the head and even at the second node; save even_head.
  3. While even and even.next both exist, splice odd to even.next and advance, then splice even to the new odd.next and advance.
  4. Attach even_head after 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. Once odd.next is 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 next must 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 head
Asked at Meesho
SensAI
Beta
Tap the mic to stop voice mode