Skip to content

Commit e7f88c4

Browse files
authored
Added task 430.
1 parent 451f287 commit e7f88c4

File tree

4 files changed

+194
-0
lines changed

4 files changed

+194
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package g0401_0500.s0430_flatten_a_multilevel_doubly_linked_list;
2+
3+
@SuppressWarnings("java:S1104")
4+
public class Node {
5+
public int val;
6+
public Node prev;
7+
public Node next;
8+
public Node child;
9+
10+
public Node(int val) {
11+
this.val = val;
12+
}
13+
14+
@Override
15+
public String toString() {
16+
return "Node{" + "val=" + val + ",next=" + next + "}";
17+
}
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g0401_0500.s0430_flatten_a_multilevel_doubly_linked_list;
2+
3+
// #Medium #Depth_First_Search #Linked_List #Doubly_Linked_List
4+
5+
/*
6+
// Definition for a Node.
7+
class Node {
8+
public int val;
9+
public Node prev;
10+
public Node next;
11+
public Node child;
12+
};
13+
*/
14+
public class Solution {
15+
// is true ONLY for the first element of the list
16+
private boolean first = true;
17+
// Holds the head node of the newly constructed doubly linked list
18+
private Node root;
19+
// Holds the current node of the newly constructed doubly linked list
20+
private Node current;
21+
22+
public Node flatten(Node head) {
23+
if (head == null) {
24+
return root;
25+
} else {
26+
// Construct our doubly linked list
27+
if (first) {
28+
first = !first;
29+
root = new Node(head.val);
30+
current = root;
31+
} else {
32+
// Map all values to the newly constructed list.
33+
// temp value to hold our prev element
34+
Node temp = current;
35+
current.next = new Node(head.val);
36+
current = current.next;
37+
current.prev = temp;
38+
}
39+
}
40+
// iterate over child nodes.
41+
if (head.child != null) {
42+
flatten(head.child);
43+
}
44+
if (head.next != null) {
45+
// iterate next
46+
flatten(head.next);
47+
}
48+
return root;
49+
}
50+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
430\. Flatten a Multilevel Doubly Linked List
2+
3+
Medium
4+
5+
You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional **child pointer**. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a **multilevel data structure** as shown in the example below.
6+
7+
Given the `head` of the first level of the list, **flatten** the list so that all the nodes appear in a single-level, doubly linked list. Let `curr` be a node with a child list. The nodes in the child list should appear **after** `curr` and **before** `curr.next` in the flattened list.
8+
9+
Return _the_ `head` _of the flattened list. The nodes in the list must have **all** of their child pointers set to_ `null`.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2021/11/09/flatten11.jpg)
14+
15+
**Input:** head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
16+
17+
**Output:** [1,2,3,7,8,11,12,9,10,4,5,6]
18+
19+
**Explanation:** The multilevel linked list in the input is shown. After flattening the multilevel linked list it becomes: ![](https://assets.leetcode.com/uploads/2021/11/09/flatten12.jpg)
20+
21+
**Example 2:**
22+
23+
![](https://assets.leetcode.com/uploads/2021/11/09/flatten2.1jpg)
24+
25+
**Input:** head = [1,2,null,3]
26+
27+
**Output:** [1,3,2]
28+
29+
**Explanation:**
30+
31+
The multilevel linked list in the input is shown.
32+
After flattening the multilevel linked list it becomes:
33+
34+
![](https://assets.leetcode.com/uploads/2021/11/24/list.jpg)
35+
36+
**Example 3:**
37+
38+
**Input:** head = []
39+
40+
**Output:** []
41+
42+
**Explanation:** There could be empty list in the input.
43+
44+
**Constraints:**
45+
46+
* The number of Nodes will not exceed `1000`.
47+
* <code>1 <= Node.val <= 10<sup>5</sup></code>
48+
49+
**How the multilevel linked list is represented in test cases:**
50+
51+
We use the multilevel linked list from **Example 1** above:
52+
53+
1---2---3---4---5---6--NULL
54+
|
55+
7---8---9---10--NULL
56+
|
57+
11--12--NULL
58+
59+
The serialization of each level is as follows:
60+
61+
[1,2,3,4,5,6,null]
62+
[7,8,9,10,null]
63+
[11,12,null]
64+
65+
To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:
66+
67+
[1, 2, 3, 4, 5, 6, null]
68+
|
69+
[null, null, 7, 8, 9, 10, null]
70+
|
71+
[ null, 11, 12, null]
72+
73+
Merging the serialization of each level and removing trailing nulls we obtain:
74+
75+
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package g0401_0500.s0430_flatten_a_multilevel_doubly_linked_list;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void flatten() {
11+
Node node1 = new Node(1);
12+
Node node2 = new Node(2);
13+
node1.next = node2;
14+
node2.prev = node1;
15+
Node node3 = new Node(3);
16+
node2.next = node3;
17+
node3.prev = node2;
18+
Node node4 = new Node(4);
19+
node3.next = node4;
20+
node4.prev = node3;
21+
Node node5 = new Node(5);
22+
node4.next = node5;
23+
node5.prev = node4;
24+
Node node6 = new Node(6);
25+
node5.next = node6;
26+
node6.prev = node5;
27+
Node node7 = new Node(7);
28+
node3.child = node7;
29+
Node node8 = new Node(8);
30+
node7.next = node8;
31+
node8.prev = node7;
32+
Node node9 = new Node(9);
33+
node8.next = node9;
34+
node9.prev = node8;
35+
Node node10 = new Node(10);
36+
node9.next = node10;
37+
node10.prev = node9;
38+
Node node11 = new Node(11);
39+
node8.child = node11;
40+
Node node12 = new Node(12);
41+
node11.next = node12;
42+
node12.prev = node11;
43+
assertThat(
44+
new Solution().flatten(node1).toString(),
45+
equalTo(
46+
"Node{val=1,next=Node{"
47+
+ "val=2,next=Node{val=3,next=Node{val=7,next=Node{val=8,next=Node{val=11,next="
48+
+ "Node{val=12,next=Node{val=9,next=Node{val=10,next=Node{val=4,next=Node{val=5"
49+
+ ",next=Node{val=6,next=null}}}}}}}}}}}}"));
50+
}
51+
}

0 commit comments

Comments
 (0)