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 nodeprev- pointer to the previous nodechild- 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
childpointers must be set tonullin the final result - Maintain proper
prevandnextconnections - 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code