2Brute2Best
🔥0

Flatten a Multilevel Doubly Linked List

Medium·LeetCode #430 Pro OnlyTricky / Edge Case
LeetCode
01

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.

Input

head, the first node of the top-level list. It can be null for an empty list.

Output

The head of the flat list. Every child pointer is null.

Example 1
Input: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 ↔ 6, where 3 has child 7 ↔ 8 ↔ 9 ↔ 10, and 8 has child 11 ↔ 12
Output: 1 ↔ 2 ↔ 3 ↔ 7 ↔ 8 ↔ 11 ↔ 12 ↔ 9 ↔ 10 ↔ 4 ↔ 5 ↔ 6

The child list of 3 goes right after 3. Inside it, the child list of 8 goes right after 8, before 9.

Example 2
Input: 1 ↔ 2 ↔ 3, where 1 has child 4
Output: 1 ↔ 4 ↔ 2 ↔ 3

The child 4 goes between 1 and its old next, 2.

Example 3
Input: head = null
Output: null

An empty list stays empty.

Constraints
  • • The list has at most 1000 nodes, counting every level.
  • • 1 ≤ Node.val ≤ 10^5
02

Pattern Recognition Clues

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

“child pointer”
→ A second kind of link to a separate list → splice that list into the main one.
“child list goes right after its parent”
→ Insert in the middle, not at the end → save next, join both ends, then keep walking.
“doubly linked, prev must be right”
→ Every join sets a prev as well as a next.
03

Approach Complexity Ladder

Brute Force — collect nodes in flat order, then rebuild every linkO(n)O(n)

Walk every level depth-first into a vector, then rewrite prev, next and child for every node

Optimal — splice each child list in as the walk reaches its parentO(n)O(1)

Walk once; when a node has a child, splice that child list in right after the node

Brute Force Intuition

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:

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.