2Brute2Best
🔥0

Odd Even Linked List

Medium·LeetCode #328 Pro OnlySlight Variation
LeetCode
01

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.

Input

head, the first node of a singly linked list (it can be empty).

Output

The head of the rearranged list.

Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [1, 3, 5, 2, 4]

Positions 1, 3, 5 hold 1, 3, 5. Positions 2 and 4 hold 2 and 4.

Example 2
Input: head = [2, 1, 3, 5, 6, 4, 7]
Output: [2, 3, 6, 7, 1, 5, 4]

Odd positions hold 2, 3, 6, 7. Even positions hold 1, 5, 4. Each group keeps its order.

Constraints
  • • The list has between 0 and 10^4 nodes.
  • • -10^6 ≤ Node.val ≤ 10^6
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“odd-indexed nodes first, then even-indexed”
→ Group by position, not by value → split the list with a walk that jumps two nodes at a time.
“relative order inside each group is kept”
→ Each chain is built in list order → relink nodes, never sort them.
“O(1) extra space”
→ No vector of node pointers → relink in place.
03

Approach Complexity Ladder

Brute Force — collect nodes in an array, then relinkO(n)O(n)

Store the node pointers in a vector, then link odd positions first and even positions after

Optimal — split into odd and even chains in place, then joinO(n)O(1)

Walk once: each odd node skips one even node and each even node skips one odd node; then join

Brute Force Intuition

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:

Split, reverse, and re-weave lists. Draw the before/after picture first.
Remember this · Linked List — Reversal
Trigger
Reverse whole / part / every k nodes, reorder, palindrome check, numbers stored as lists.
Think
Which piece do I reverse, and which nodes must I remember to reconnect afterwards?
Key question
Before reversing, who is the node just before the piece and who is just after?
Brute2Best Pro Content

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.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Linked List — Reversal translate into production architectures and low-level memory mechanics.