2Brute2Best
🔥0

Reverse a Doubly Linked List

Easy·GeeksforGeeks Pro OnlyBasic Reinforcement
GeeksforGeeks
01

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.

Input

head, the first node of a doubly linked list. The list can be empty.

Output

The head of the reversed list. Its prev must be null.

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

Reading forwards gives 4, 3, 2, 1. Reading backwards from the new tail gives 1, 2, 3, 4 again.

Example 2
Input: head = [7]
Output: [7]

One node is already reversed. Its prev and next stay null.

Example 3
Input: head = []
Output: null

An empty list has no head to return.

Constraints
  • • 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.
02

Pattern Recognition Clues

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

“doubly linked list”
→ Each node already has a `prev` arrow, so we don't need to walk back. Reversing means trading the two arrows on each node.
“reverse”
→ Every node must change. A swap per node is enough, and no node needs to be moved to a new place.
“return the new head”
→ The old tail becomes the front. Its `prev` is null after the swap, which marks it as the head.
03

Approach Complexity Ladder

Brute Force — stack of nodes, relink both arrowsO(N)O(N)

Push every node on a stack, then pop them and set both arrows again

Optimal — swap prev and next on every nodeO(N)O(1)

Walk once, swap each node's prev and next, return the old tail

Brute Force Intuition

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:

Walk once, pointing each node back to the previous one.
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.