Flatten a Multilevel Doubly Linked List - Problem

Transform a Multi-Level Maze into a Single Path

Imagine you have a doubly linked list that's not just a simple chain, but a complex multi-level structure! Each node can have:
  • next - pointer to the next node
  • prev - pointer to the previous node
  • child - pointer to a separate doubly linked list branch

Your mission is to flatten this multilevel structure into a single-level doubly linked list. The key rule: when you encounter a node with a child, the entire child branch should be inserted between the current node and its next node.

Think of it like exploring a building with multiple floors - you want to create a single path that visits every room in a depth-first manner, then return to continue the main corridor.

Requirements:
  • All child pointers must be set to null in the final result
  • Maintain proper prev and next connections
  • Return the head of the flattened list

Input & Output

example_1.py โ€” Python
$ Input: head = [1,2,3,4,5,6,null,null,null,7,8,11,12,null,null,null,null] Multilevel structure: 1---2---3---4---5---6--NULL | 7---8---11--12--NULL
โ€บ Output: [1,2,3,7,8,11,12,4,5,6] Flattened: 1<->2<->3<->7<->8<->11<->12<->4<->5<->6
๐Ÿ’ก Note: Node 3 has a child pointing to node 7. The child branch (7,8,11,12) is inserted between node 3 and node 4 in depth-first order.
example_2.py โ€” Python
$ Input: head = [1,2,null,3,null,null,null] Multilevel structure: 1---2--NULL | 3--NULL
โ€บ Output: [1,3,2] Flattened: 1<->3<->2
๐Ÿ’ก Note: Node 1 has a child pointing to node 3. Since node 1's child branch contains only node 3, it's inserted between node 1 and node 2.
example_3.py โ€” Python
$ Input: head = [1,2,3,4,5,6,null,null,null,7,8,null,null,9,10,null,null] Complex multilevel with nested children
โ€บ Output: [1,2,3,7,8,9,10,4,5,6] Flattened result with all levels merged in DFS order
๐Ÿ’ก Note: Multiple levels of children are processed depth-first: when we encounter a child, we fully explore its subtree before continuing with the main branch.

Constraints

  • The number of nodes in the list is in the range [0, 1000]
  • 1 โ‰ค Node.val โ‰ค 105
  • All child pointers must be set to null in the result
  • The input multilevel linked list is well-formed (no cycles)

Visualization

Tap to expand
๐Ÿข Building Navigation with Stack MemoryMulti-level BuildingRoom 1Room 2Room 7Room 8Room 3Room 4Stack (Sticky Notes)"Go to Room 3"Remember whereto continueExploration Path (DFS):127834๐ŸŸข Normal path ๐ŸŸ  From stack (sticky note)๐ŸŽฏ **Key Insight:** Stack remembers 'return addresses' for perfect DFS traversal
Understanding the Visualization
1
Enter Room with Stairs
When you find a room with stairs (child pointer), write down where you would have gone next on a sticky note
2
Explore Sub-floor Completely
Go down the stairs and explore that entire floor, connecting rooms as you go
3
Return Using Sticky Note
When you reach a dead end, use your sticky note to return and continue the main path
4
Repeat Until Done
Continue this process until you've visited every room and created one continuous path
Key Takeaway
๐ŸŽฏ Key Insight: A stack naturally handles the 'return address' problem in multilevel structures, enabling efficient depth-first flattening in a single pass.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
68.0K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen