Reverse a Doubly Linked List
Problem Statement
You are given the head of a doubly linked list. Every node has a prev arrow and a next arrow. Reverse the list, so the last node becomes the first, and fix both arrows on every node. Return the new head.
Each node also has a child field. Ignore it here: it is always null in this problem.
head, the first node of a doubly linked list. The list can be empty.
The head of the reversed list. Its prev must be null.
Reading forwards gives 4, 3, 2, 1. Reading backwards from the new tail gives 1, 2, 3, 4 again.
One node is already reversed. Its prev and next stay null.
An empty list has no head to return.
- • The list has between 0 and 10^5 nodes.
- • Values fit in a 32-bit signed integer.
- • Every node's prev and next must be correct after the reversal.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Push every node on a stack, then pop them and set both arrows again
Walk once, swap each node's prev and next, return the old tail
Use a stack to get the nodes in reverse order. Pop them one by one. For each popped node, set its prev to the node popped before it, and set the previous node's next to this one. The answer is right, but the stack holds every node at once.
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.