-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJuly10_2020.java
35 lines (28 loc) · 1020 Bytes
/
July10_2020.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
// Flatten the double link list - traverse next until node has child.
class Solution {
public Node flatten(Node head) {
Stack<Node> stack = new Stack<>();
Node node = head, next = null;
while(node != null){
if(node.child != null){
// setting next to stack
if(node.next != null){
next = node.next;
next.prev = null;
stack.push(next);
}
// setting child to list
node.next = node.child;
node.next.prev = node;
node.child = null;
}
if(node.next == null && stack.size() > 0){
next = stack.pop();
node.next = next;
next.prev = node;
}
node = node.next;
}
return head;
}
}