Flatten a Multilevel Doubly Linked List
Flatten a Multilevel Doubly Linked List is LeetCode 430 (Medium). Each node has next and prev pointers, plus a child pointer that may lead to another doubly linked list, whose nodes can have children of their own. Flatten everything into a single level: a node's child list comes right after the node and before the node's old next. Return the head, with every child pointer set to null.
- The list can be empty; then the answer is null.
- The number of nodes will not exceed 1000
- 1 <= Node.val <= 10⁵
Intuition
The required order is: a node, then its whole child list (flattened the same way), then whatever came after the node. That is a depth-first order, and it can be built in place, because once a child list is spliced in, it is simply part of the main list.
So walk the main list from the head. When the current node has a child, find the last node of the child list, hook the child list in after the current node, and attach the old next after that last node. Then keep walking. A node inside the spliced list that has its own child is reached later by the same walk and gets the same treatment, so deeper levels need no stack and no recursion.
A pointer that branches off a list and must be visited before the rest of the list is a depth-first order. When the structure can be rewired, splicing each branch in where it starts turns the depth-first order into a plain walk.
Approach
Before reading on, draw the list 1-2-3-4-5-6 where 3 has the child 7-8-9-10 and 8 has the child 11-12. Write the flattened order, then list every pointer that changes when 7-8-9-10 is spliced in after 3.
Two ways to solve it
Walk the main list; at a node with a child, find the child list's last node and splice the whole list in.
- No extra memory: only a few pointers.
- Deeper levels for free: the walk reaches them after the splice.
The usual follow-up answer.
Flatten each child list with a recursive call that returns its last node, then link that node to the old next.
- Reads like the definition: flatten the child, then carry on.
- Cost: one call frame per level of nesting,
dlevels deep.
The first idea most people write.
Both produce the same order in O(n) time. The splice needs no extra memory; the recursion keeps one call open per level of nesting. The steps, code and demo follow the splice, and the recursive code comes after the demo.
Walk the main list from the head
Start cur at head and move with cur = cur.next until it is null. Every child list ends up spliced into this path before cur gets there, so one walk reaches every node on every level.
Find the last node of the child list
When cur.child is set, start tail at the child and follow next until tail.next is null. Only the child's own level is walked; any grandchildren still hang off their nodes and are handled when cur reaches them.
Splice the child list in
Save nxt = cur.next first. Then link cur.next = cur.child and cur.child.prev = cur, and set cur.child to null, since the flattened list must not keep any child pointers.
Attach the old next after the tail
Set tail.next = nxt, and when nxt exists, nxt.prev = tail. The rest of the original level now follows the whole child list, which is exactly the depth-first order. Then move cur on as usual.
Code in Python, C++ and Java
cur moves to 2.cur moves to 3.cur will deal with it once it gets there.cur moves on to 7, the start of the spliced list.cur moves to 8.cur moves on to 11, the start of the spliced list.cur moves to 12.cur moves to 9.cur moves to 10.cur moves to 4.cur moves to 5.cur moves to 6.cur becomes null and the loop stops.prev pointing back to the node before it.cur moves on to 3, the start of the spliced list.cur moves to 2.cur becomes null and the loop stops.prev pointing back to the node before it.cur moves to 2.cur moves to 3.cur moves on to 4, the start of the spliced list.cur moves to 5.cur moves on to 6, the start of the spliced list.cur becomes null and the loop stops.prev pointing back to the node before it.Recursive depth-first search
dfs(node) flattens the level that starts at node and returns its last node. At a node with a child, it flattens the child list first, splices it in, and links the returned last node to the old next. The recursion goes one call deeper only for a child, so the stack holds one frame per level of nesting.
Common pitfalls
Overwriting next before saving it
cur.next = cur.child cur.child.prev = cur tail.next = cur.next
nxt = cur.next cur.next = cur.child cur.child.prev = cur tail.next = nxt
After the first line cur.next already points at the child. On 1-2-3-4-5-6 with 3's child 7-8-9-10, node 10 gets linked back to 7, making a loop, and 4, 5 and 6 are cut off for good.
Fixing next pointers but not prev
cur.next = cur.child tail.next = nxt
cur.next = cur.child
cur.child.prev = cur
tail.next = nxt
if nxt:
nxt.prev = tailRead forwards, the list looks right. But 7's prev is still null and 4's prev still points at 3, so walking backwards from 4 jumps straight to 3 and skips the whole child list. The result is not a valid doubly linked list.
Leaving the child pointer set
cur.next = cur.child cur.child.prev = cur
cur.next = cur.child cur.child.prev = cur cur.child = None
Node 3 would still reach 7 through child, so the list is still multilevel. The problem asks for every child pointer to be null in the result.
Edge cases
Its old next is null, so nothing has to follow the child list. The if nxt check skips nxt.prev = tail, which would otherwise fail on a null node. 1-2-3 with 3's child 4-5 becomes 1-2-3-4-5.
head is null, so the loop never runs and null is returned.
Complexity
cur visits every node once. Each child list is walked once to find its last node, and every list is spliced only once, so those walks add at most one more visit per node. Only a few pointers are stored.Flatten a Multilevel Doubly Linked List FAQ
Can I use a stack instead?
Yes. When a node has a child, push its old next onto a stack and move into the child; when you reach a node with no next, pop the saved node and link it on. It is also O(n), but the stack holds one saved node per level of nesting, while the splice needs none.
How is this related to Flatten Binary Tree to Linked List?
Both produce a depth-first order in place. In Flatten Binary Tree to Linked List the left subtree goes between a node and its right subtree, just as the child list goes between a node and its next here. The tree version has no prev pointers to repair.