The trick here is to name the state correctly, then let the implementation follow. For 430. Flatten a Multilevel Doubly Linked List, the solution in this repository is mainly a stack solution.
Guide
What?
Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The solution is organized around the main LeetCode entry point and a few local helpers.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 97.09% of C++ online submissions for Flatten a Multilevel Doubly Linked List.
02//Memory Usage: 7.5 MB, less than 36.59% of C++ online submissions for Flatten a Multilevel Doubly Linked List.
03/*
04// Definition for a Node.
05class Node {
06public:
07 int val;
08 Node* prev;
09 Node* next;
10 Node* child;
11};
12*/
13
14class Solution {
15public:
16 Node* flatten(Node* head) {
17 if(head == nullptr) return nullptr;
18
19 Node* cur;
20 stack<Node*> stk;
21
22 stk.push(head);
23
24 Node* newhead = head;
25
26 while(!stk.empty()){
27 //cur
28 cur = stk.top(); stk.pop();
29
30 //go to the end of list or the node having child
31 while(cur->next != nullptr && cur->child == nullptr){
32 cur = cur->next;
33 }
34
35 if(cur->next == nullptr && cur->child != nullptr){
36 //end node and has child
37 cur->next = cur->child;
38 cur->child->prev = cur;
39 cur->child = nullptr;
40 //need to further visit cur->next
41 stk.push(cur->next);
42 }else if(cur->next == nullptr){
43 //go to the end of current level
44 if(!stk.empty()){
45 //stk.top() is the node to be concatenated
46 cur->next = stk.top();
47 stk.top()->prev = cur;
48 }
49 }else{
50 //go to the node having child
51
52 //we will later visit its child and then its next
53 stk.push(cur->next);
54 stk.push(cur->child);
55
56 //rewind
57 cur->next = cur->child;
58 cur->child->prev = cur;
59 cur->child = nullptr;
60 }
61 }
62
63 return newhead;
64 }
65};
Cost