-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ430FlattenList.java
48 lines (44 loc) · 1.47 KB
/
Q430FlattenList.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
45
46
47
/*
@b-knd (jingru) on 02 August 2022 11:08:00
*/
class Solution {
public Node flatten(Node head) {
flattenTail(head);
return head;
}
public Node flattenTail(Node head){
//first case: head is null, end of the list, return head
if(head == null){
return head;
}
//second case: head does not have child, proceed to flatten next if available or simply return head
else if(head.child == null){
if(head.next == null){
return head;
}
return flattenTail(head.next);
}
/*
third case: head has child
- flatten child to find child tail (recursively) and remove child from head
- if there is no next node, return tail
- if next node exists, update link and continue flatten list
*/
else{
Node child = head.child;
head.child = null;
Node next = head.next;
Node tail = flattenTail(child);
head.next = child;
child.prev = head;
if(next != null){
tail.next = next;
next.prev = tail;
return flattenTail(next);
}
return tail;
}
}
}
//Runtime: 0 ms, faster than 100.00% of Java online submissions for Flatten a Multilevel Doubly Linked List.
//Memory Usage: 43.1 MB, less than 5.75% of Java online submissions for Flatten a Multilevel Doubly Linked List.