-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm430.java
44 lines (37 loc) · 898 Bytes
/
m430.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
// Definition for a Node.
class Node {
public int val;
public Node prev;
public Node next;
public Node child;
};
*/
class Solution {
public Node flatten(Node head) {
if (head == null) {
return null;
}
if (head.child == null) {
head.next = flatten(head.next);
return head;
}
if (head.next == null) {
head.next = flatten(head.child);
head.child = null;
head.next.prev = head;
return head;
}
Node holder = head.next;
head.next = flatten(head.child);
head.next.prev = head;
head.child = null;
Node output = head;
while (head.next != null) {
head = head.next;
}
head.next = flatten(holder);
head.next.prev = head;
return output;
}
}