We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 43cd3c3 commit 8710946Copy full SHA for 8710946
solution/0430.Flatten a Multilevel Doubly Linked List/Solution.java
@@ -0,0 +1,32 @@
1
+class Solution {
2
+ public Node flatten(Node head) {
3
+ if (head == null) {
4
+ return null;
5
+ }
6
+ dfs(head);
7
+ head.prev = null;
8
+ return head;
9
10
+
11
+ private Node dfs(Node head) {
12
+ Node cur = head;
13
+ while (cur != null) {
14
+ head.prev = cur;
15
+ Node next = cur.next;
16
+ if (cur.child != null) {
17
+ Node h = dfs(cur.child);
18
+ cur.child = null;
19
+ Node t = h.prev;
20
+ cur.next = h;
21
+ h.prev = cur;
22
+ t.next = next;
23
+ if (next != null) {
24
+ next.prev = t;
25
26
+ head.prev = t;
27
28
+ cur = next;
29
30
31
32
+}
0 commit comments