Skip to content

Commit 2a1a8e4

Browse files
committed
feat: add solutions to lc problem: No.0430. Flatten a Multilevel Doubly Linked List
1 parent 658d9a7 commit 2a1a8e4

File tree

5 files changed

+186
-35
lines changed

5 files changed

+186
-35
lines changed

solution/0100-0199/0160.Intersection of Two Linked Lists/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
<p><strong>示例 1:</strong></p>
2020

21-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1.png" style="height: 130px; width: 400px;"></a></p>
21+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_1.png" style="height: 130px; width: 400px;"></a></p>
2222

2323
<pre><strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
2424
<strong>输出:</strong>Reference of the node with value = 8
@@ -29,7 +29,7 @@
2929

3030
<p><strong>示例&nbsp;2:</strong></p>
3131

32-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" style="height: 136px; width: 350px;"></a></p>
32+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_2.png" style="height: 136px; width: 350px;"></a></p>
3333

3434
<pre><strong>输入:</strong>intersectVal&nbsp;= 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
3535
<strong>输出:</strong>Reference of the node with value = 2
@@ -40,7 +40,7 @@
4040

4141
<p><strong>示例&nbsp;3:</strong></p>
4242

43-
<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" style="height: 126px; width: 200px;"></a></p>
43+
<p><a href="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" target="_blank"><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0160.Intersection%20of%20Two%20Linked%20Lists/images/160_example_3.png" style="height: 126px; width: 200px;"></a></p>
4444

4545
<pre><strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
4646
<strong>输出:</strong>null

solution/0400-0499/0430.Flatten a Multilevel Doubly Linked List/README.md

+64-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
<li><code>1 &lt;= Node.val &lt;= 10^5</code></li>
8686
</ul>
8787

88-
8988
## 解法
9089

9190
<!-- 这里可写通用的实现逻辑 -->
@@ -97,15 +96,77 @@
9796
<!-- 这里可写当前语言的特殊实现逻辑 -->
9897

9998
```python
100-
99+
"""
100+
# Definition for a Node.
101+
class Node:
102+
def __init__(self, val, prev, next, child):
103+
self.val = val
104+
self.prev = prev
105+
self.next = next
106+
self.child = child
107+
"""
108+
109+
class Solution:
110+
def flatten(self, head: 'Node') -> 'Node':
111+
def preorder(pre, cur):
112+
if cur is None:
113+
return pre
114+
cur.prev = pre
115+
pre.next = cur
116+
117+
t = cur.next
118+
tail = preorder(cur, cur.child)
119+
cur.child = None
120+
return preorder(tail, t)
121+
122+
if head is None:
123+
return None
124+
dummy = Node(0, None, head, None)
125+
preorder(dummy, head)
126+
dummy.next.prev = None
127+
return dummy.next
101128
```
102129

103130
### **Java**
104131

105132
<!-- 这里可写当前语言的特殊实现逻辑 -->
106133

107134
```java
108-
135+
/*
136+
// Definition for a Node.
137+
class Node {
138+
public int val;
139+
public Node prev;
140+
public Node next;
141+
public Node child;
142+
};
143+
*/
144+
145+
class Solution {
146+
public Node flatten(Node head) {
147+
if (head == null) {
148+
return null;
149+
}
150+
Node dummy = new Node();
151+
dummy.next = head;
152+
preorder(dummy, head);
153+
dummy.next.prev = null;
154+
return dummy.next;
155+
}
156+
157+
private Node preorder(Node pre, Node cur) {
158+
if (cur == null) {
159+
return pre;
160+
}
161+
cur.prev = pre;
162+
pre.next = cur;
163+
164+
Node t = cur.next;
165+
Node tail = preorder(cur, cur.child);
166+
cur.child = null;
167+
return preorder(tail, t);
168+
}
169+
}
109170
```
110171

111172
### **...**

solution/0400-0499/0430.Flatten a Multilevel Doubly Linked List/README_EN.md

+64-6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
<p>You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. 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.</p>
88

9-
10-
119
<p>Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.</p>
1210

13-
1411
<p>&nbsp;</p>
1512
<p><strong>Example 1:</strong></p>
1613

@@ -91,21 +88,82 @@ After flattening the multilevel linked list it becomes:
9188
<li><code>1 &lt;= Node.val &lt;= 10<sup>5</sup></code></li>
9289
</ul>
9390

94-
9591
## Solutions
9692

9793
<!-- tabs:start -->
9894

9995
### **Python3**
10096

10197
```python
102-
98+
"""
99+
# Definition for a Node.
100+
class Node:
101+
def __init__(self, val, prev, next, child):
102+
self.val = val
103+
self.prev = prev
104+
self.next = next
105+
self.child = child
106+
"""
107+
108+
class Solution:
109+
def flatten(self, head: 'Node') -> 'Node':
110+
def preorder(pre, cur):
111+
if cur is None:
112+
return pre
113+
cur.prev = pre
114+
pre.next = cur
115+
116+
t = cur.next
117+
tail = preorder(cur, cur.child)
118+
cur.child = None
119+
return preorder(tail, t)
120+
121+
if head is None:
122+
return None
123+
dummy = Node(0, None, head, None)
124+
preorder(dummy, head)
125+
dummy.next.prev = None
126+
return dummy.next
103127
```
104128

105129
### **Java**
106130

107131
```java
108-
132+
/*
133+
// Definition for a Node.
134+
class Node {
135+
public int val;
136+
public Node prev;
137+
public Node next;
138+
public Node child;
139+
};
140+
*/
141+
142+
class Solution {
143+
public Node flatten(Node head) {
144+
if (head == null) {
145+
return null;
146+
}
147+
Node dummy = new Node();
148+
dummy.next = head;
149+
preorder(dummy, head);
150+
dummy.next.prev = null;
151+
return dummy.next;
152+
}
153+
154+
private Node preorder(Node pre, Node cur) {
155+
if (cur == null) {
156+
return pre;
157+
}
158+
cur.prev = pre;
159+
pre.next = cur;
160+
161+
Node t = cur.next;
162+
Node tail = preorder(cur, cur.child);
163+
cur.child = null;
164+
return preorder(tail, t);
165+
}
166+
}
109167
```
110168

111169
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node prev;
6+
public Node next;
7+
public Node child;
8+
};
9+
*/
10+
111
class Solution {
212
public Node flatten(Node head) {
313
if (head == null) {
414
return null;
515
}
6-
dfs(head);
7-
head.prev = null;
8-
return head;
16+
Node dummy = new Node();
17+
dummy.next = head;
18+
preorder(dummy, head);
19+
dummy.next.prev = null;
20+
return dummy.next;
921
}
1022

11-
private Node dfs(Node head) {
12-
Node cur = head;
13-
while (cur != null) {
14-
head.prev = cur;
15-
Node next = cur.next;
16-
if (cur.child != null) {
17-
Node h = dfs(cur.child);
18-
cur.child = null;
19-
Node t = h.prev;
20-
cur.next = h;
21-
h.prev = cur;
22-
t.next = next;
23-
if (next != null) {
24-
next.prev = t;
25-
}
26-
head.prev = t;
27-
}
28-
cur = next;
23+
private Node preorder(Node pre, Node cur) {
24+
if (cur == null) {
25+
return pre;
2926
}
30-
return head;
27+
cur.prev = pre;
28+
pre.next = cur;
29+
30+
Node t = cur.next;
31+
Node tail = preorder(cur, cur.child);
32+
cur.child = null;
33+
return preorder(tail, t);
3134
}
32-
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val, prev, next, child):
5+
self.val = val
6+
self.prev = prev
7+
self.next = next
8+
self.child = child
9+
"""
10+
11+
class Solution:
12+
def flatten(self, head: 'Node') -> 'Node':
13+
def preorder(pre, cur):
14+
if cur is None:
15+
return pre
16+
cur.prev = pre
17+
pre.next = cur
18+
19+
t = cur.next
20+
tail = preorder(cur, cur.child)
21+
cur.child = None
22+
return preorder(tail, t)
23+
24+
if head is None:
25+
return None
26+
dummy = Node(0, None, head, None)
27+
preorder(dummy, head)
28+
dummy.next.prev = None
29+
return dummy.next

0 commit comments

Comments
 (0)