-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSolution430.java
49 lines (41 loc) · 1.32 KB
/
Solution430.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
48
49
package leetcode.linkedlist;
public class Solution430 {
static class Node {
public int val;
public Node prev;
public Node next;
public Node child;
}
public Node flatten(Node head) {
doFlatten(head);
return head;
}
private Node doFlatten(Node head) {
Node last = null;
while (head != null) {
// 记录下来最后的节点,作为返回结果供之后的递归拼接使用
last = head;
// 如果当前节点没有 child 节点,直接向后移动
if (head.child == null) {
head = head.next;
} else {
// 有 child 节点,记录下来右边待拼接的节点
Node right = head.next;
// 连接 child 头节点
head.next = head.child;
head.child.prev = head;
// child 节点扁平化
Node childLast = doFlatten(head.child);
head.child = null;
// 拼接 right
childLast.next = right;
if (right != null) {
right.prev = childLast;
}
// 移动 head 节点之后继续遍历
head = childLast;
}
}
return last;
}
}