Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7e8f781

Browse files
solves flatten multilevel doubly linked list
1 parent fec4381 commit 7e8f781

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number) | [![Java](assets/java.png)](src/ThirdMaximumNumber.java) [![Python](assets/python.png)](python/fizz_buzz.py) | |
287287
| 415 | [Add Strings](https://leetcode.com/problems/add-strings) | [![Java](assets/java.png)](src/AddString.java) [![Python](assets/python.png)](python/add_strings.py) | |
288288
| 422 | 🔒 [Valid Word Square](https://leetcode.com/problems/valid-word-square) | | |
289+
| 430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list) | [![Java](assets/java.png)](src/FlattenAMultiLevelDoublyLinkedList.java) | |
289290
| 434 | [Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string) | [![Java](assets/java.png)](src/NumberOfSegmentsInString.java) [![Python](assets/python.png)](python/number_of_segments_in_a_string.py) | |
290291
| 441 | [Arranging Coins](https://leetcode.com/problems/arranging-coins) | [![Java](assets/java.png)](src/ArrangingCoins.java) [![Python](assets/python.png)](python/arranging_coins.py) | |
291292
| 443 | [String Compression](https://leetcode.com/problems/string-compression) | [![Java](assets/java.png)](src/StringCompression.java) [![Python](assets/python.png)](python/string_compression.py) | |
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// T: O(n)
2+
// S: O(n)
3+
4+
public class FlattenAMultiLevelDoublyLinkedList {
5+
private static final class Node {
6+
public int val;
7+
public Node prev;
8+
public Node next;
9+
public Node child;
10+
}
11+
12+
public Node flatten(Node head) {
13+
for (Node temp = head ; temp != null ; temp = temp.next) {
14+
if (temp.child != null) {
15+
Node lastNodeOfFlattenedChildren = flattenLinkedList(temp.child);
16+
lastNodeOfFlattenedChildren.next = temp.next;
17+
if (temp.next != null) {
18+
temp.next.prev = lastNodeOfFlattenedChildren;
19+
}
20+
temp.next = temp.child;
21+
temp.child.prev = temp;
22+
temp.child = null;
23+
}
24+
}
25+
return head;
26+
}
27+
28+
private Node flattenLinkedList(Node head) {
29+
Node temp = head;
30+
for (; temp.next != null ; temp = temp.next) {
31+
if (temp.child != null) {
32+
Node lastNodeOfFlattenedChildren = flattenLinkedList(temp.child);
33+
lastNodeOfFlattenedChildren.next = temp.next;
34+
temp.next.prev = lastNodeOfFlattenedChildren;
35+
temp.next = temp.child;
36+
temp.child.prev = temp;
37+
temp.child = null;
38+
}
39+
}
40+
if (temp.child != null) {
41+
Node lastNodeOfFlattenedChildren = flattenLinkedList(temp.child);
42+
temp.next = temp.child;
43+
temp.child.prev = temp;
44+
temp.child = null;
45+
return lastNodeOfFlattenedChildren;
46+
}
47+
return temp;
48+
}
49+
}

0 commit comments

Comments
 (0)