Flatten a Multilevel Doubly Linked List
Problem Statement
You are given the head of a doubly linked list. Each node has a prev pointer, a next pointer, and a child pointer. A child can point to the head of another doubly linked list, and that list can have its own children, and so on.
Flatten the whole structure into one doubly linked list. For a node that has a child, the child's list goes right after that node, and before the node's old next. Do this at every level. When you are done, every child must be null, and every prev and next must be correct. Return the head of the flat list.
head, the first node of the top-level list. It can be null for an empty list.
The head of the flat list. Every child pointer is null.
The child list of 3 goes right after 3. Inside it, the child list of 8 goes right after 8, before 9.
The child 4 goes between 1 and its old next, 2.
An empty list stays empty.
- • The list has at most 1000 nodes, counting every level.
- • 1 ≤ Node.val ≤ 10^5
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Walk every level depth-first into a vector, then rewrite prev, next and child for every node
Walk once; when a node has a child, splice that child list in right after the node
Work out the order the nodes should appear in: a node, then its child list (in this same order), then the rest of its own level. Put the nodes in a vector in that order. Then give every node a prev and next from its neighbours in the vector, and set child to null.
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.