2Brute2Best
🔥0

Rotate Doubly Linked List by N Nodes

Medium·GeeksforGeeks Pro OnlyPattern Transfer
GeeksforGeeks
01

Problem Statement

You are given the head of a doubly linked list and a number N. Rotate the list counter-clockwise by N nodes. That means the first N nodes move to the back, in the same order they were in. Return the new head.

Every node has a prev and a next pointer. After the rotation, both must be correct for every node. The new head has no node before it, and the new last node has no node after it.

Input

head, the first node of a doubly linked list, and N, a positive number smaller than the number of nodes.

Output

The new head of the rotated list.

Example 1
Input: head = 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5, N = 2
Output: 3 ↔ 4 ↔ 5 ↔ 1 ↔ 2

The first two nodes, 1 and 2, move to the back. The new head is 3.

Example 2
Input: head = 2 ↔ 6 ↔ 5 ↔ 4, N = 3
Output: 4 ↔ 2 ↔ 6 ↔ 5

The first three nodes, 2, 6 and 5, move to the back. The new head is 4.

Constraints
  • • The list has between 2 and 10^5 nodes.
  • • 1 ≤ N < the number of nodes.
  • • Node values fit in a 32-bit int.
02

Pattern Recognition Clues

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

“doubly linked list”
→ Two arrows per node → every relink must set a next and a prev that agree.
“rotate counter-clockwise by N”
→ The first N nodes move to the back → one join and one cut.
“N is smaller than the number of nodes”
→ No full turns to remove → N is a direct cut position, the N-th node.
03

Approach Complexity Ladder

Brute Force — copy the nodes in rotated order, then relink every nodeO(n)O(n)

Store the nodes in their new order, then set prev and next for every node from its neighbours

Optimal — close into a circle, cut after N, fix four pointersO(n)O(1)

Walk to the tail, join tail and head both ways, cut after the N-th node, clear the new head's prev

Brute Force Intuition

The easiest way to be sure every arrow is right is to rebuild all of them. Store the nodes in the rotated order in a vector. Then give each node a prev and a next from its neighbours in that vector.

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.