-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path763.partition-labels.java
More file actions
45 lines (41 loc) · 1.03 KB
/
Copy path763.partition-labels.java
File metadata and controls
45 lines (41 loc) · 1.03 KB
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
45
/*
// 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;
Node res = null;//new Node(-1);
Node res1 = null;//new Node(-1);
Stack<Node> stk = new Stack<Node>();
while(head != null){
if(res1 == null){
res1 = new Node(head.val);
res = res1;
}
else{
res1.next = new Node(head.val);
// System.out.println(head.val);
res1.next.prev = res1;
res1= res1.next;
}
if(head.child != null){
stk.push(head);
head = head.child;
}
else{
head = head.next;
}
if(head == null && !stk.isEmpty()){
head = stk.pop().next;
}
}
return res;
}
}