Odd Even Linked List
Problem Statement
You are given the head of a singly linked list. Count the positions from 1 at the head. Rearrange the list so that all nodes at odd positions come first, followed by all nodes at even positions. Keep the original order inside each group. Return the new head.
Relink the existing nodes. Do not swap values between nodes. The solution must use O(1) extra space and O(n) time.
head, the first node of a singly linked list (it can be empty).
The head of the rearranged list.
Positions 1, 3, 5 hold 1, 3, 5. Positions 2 and 4 hold 2 and 4.
Odd positions hold 2, 3, 6, 7. Even positions hold 1, 5, 4. Each group keeps its order.
- • The list has between 0 and 10^4 nodes.
- • -10^6 ≤ Node.val ≤ 10^6
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Store the node pointers in a vector, then link odd positions first and even positions after
Walk once: each odd node skips one even node and each even node skips one odd node; then join
Picking nodes by position is easy in an array: index 0, 2, 4 are the odd positions. So store every node in a vector, put the odd-position nodes first and the even-position nodes after, then link them in that order.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.