Skip to content

Commit 92df1fd

Browse files
committed
feat: add solutions to lcof2 problems: No.028,029
1 parent ddebd1a commit 92df1fd

File tree

6 files changed

+279
-4
lines changed

6 files changed

+279
-4
lines changed

Diff for: lcof2/剑指 Offer II 028. 展平多级双向链表/README.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,92 @@
9999

100100
<!-- 这里可写通用的实现逻辑 -->
101101

102+
仔细观察一下这个结构,不难发现其实就是前序遍历二叉树
103+
102104
<!-- tabs:start -->
103105

104106
### **Python3**
105107

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

108110
```python
109-
111+
"""
112+
# Definition for a Node.
113+
class Node:
114+
def __init__(self, val, prev, next, child):
115+
self.val = val
116+
self.prev = prev
117+
self.next = next
118+
self.child = child
119+
"""
120+
121+
class Solution:
122+
def flatten(self, head: 'Node') -> 'Node':
123+
if head is None:
124+
return None
125+
dummy = Node()
126+
tail = dummy
127+
128+
def preOrder(node: 'Node'):
129+
nonlocal tail
130+
if node is None:
131+
return
132+
next = node.next
133+
child = node.child
134+
tail.next = node
135+
node.prev = tail
136+
tail = tail.next
137+
node.child = None
138+
preOrder(child)
139+
preOrder(next)
140+
141+
preOrder(head)
142+
dummy.next.prev = None
143+
return dummy.next
110144
```
111145

112146
### **Java**
113147

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

116150
```java
117-
151+
/*
152+
// Definition for a Node.
153+
class Node {
154+
public int val;
155+
public Node prev;
156+
public Node next;
157+
public Node child;
158+
};
159+
*/
160+
161+
class Solution {
162+
private Node dummy = new Node();
163+
private Node tail = dummy;
164+
165+
public Node flatten(Node head) {
166+
if (head == null) {
167+
return null;
168+
}
169+
preOrder(head);
170+
dummy.next.prev = null;
171+
return dummy.next;
172+
}
173+
174+
private void preOrder(Node node) {
175+
if (node == null) {
176+
return;
177+
}
178+
Node next = node.next;
179+
Node child = node.child;
180+
tail.next = node;
181+
node.prev = tail;
182+
tail = tail.next;
183+
node.child = null;
184+
preOrder(child);
185+
preOrder(next);
186+
}
187+
}
118188
```
119189

120190
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
11+
class Solution {
12+
private Node dummy = new Node();
13+
private Node tail = dummy;
14+
15+
public Node flatten(Node head) {
16+
if (head == null) {
17+
return null;
18+
}
19+
preOrder(head);
20+
dummy.next.prev = null;
21+
return dummy.next;
22+
}
23+
24+
private void preOrder(Node node) {
25+
if (node == null) {
26+
return;
27+
}
28+
Node next = node.next;
29+
Node child = node.child;
30+
tail.next = node;
31+
node.prev = tail;
32+
tail = tail.next;
33+
node.child = null;
34+
preOrder(child);
35+
preOrder(next);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
if head is None:
14+
return None
15+
dummy = Node()
16+
tail = dummy
17+
18+
def preOrder(node: 'Node'):
19+
nonlocal tail
20+
if node is None:
21+
return
22+
next = node.next
23+
child = node.child
24+
tail.next = node
25+
node.prev = tail
26+
tail = tail.next
27+
node.child = None
28+
preOrder(child)
29+
preOrder(next)
30+
31+
preOrder(head)
32+
dummy.next.prev = None
33+
return dummy.next

Diff for: lcof2/剑指 Offer II 029. 排序的循环链表/README.md

+71-2
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,91 @@
6161

6262
<!-- 这里可写通用的实现逻辑 -->
6363

64+
1. 头节点如果为空,直接返回 `node`
65+
2. 如果 `insertVal` 在链表的最小值和最大值之间,找到合适的位置插入
66+
3. 如果 `insertVal` 小于链表的最小值或大于链表的最大值,则在头节点和尾节点之间插入
67+
4. 链表的所有值和 `insertVal` 都相等,任意位置插入
68+
6469
<!-- tabs:start -->
6570

6671
### **Python3**
6772

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

7075
```python
71-
76+
"""
77+
# Definition for a Node.
78+
class Node:
79+
def __init__(self, val=None, next=None):
80+
self.val = val
81+
self.next = next
82+
"""
83+
84+
class Solution:
85+
def insert(self, head: 'Node', insertVal: int) -> 'Node':
86+
node = Node(insertVal)
87+
if head is None:
88+
node.next = node
89+
return node
90+
p = head
91+
while True:
92+
if p.val <= insertVal and insertVal <= p.next.val or \
93+
p.val > p.next.val and (insertVal <= p.next.val or insertVal >= p.val) or \
94+
p.next == head:
95+
96+
node.next = p.next
97+
p.next = node
98+
break
99+
100+
p = p.next
101+
return head
72102
```
73103

74104
### **Java**
75105

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

78108
```java
79-
109+
/*
110+
// Definition for a Node.
111+
class Node {
112+
public int val;
113+
public Node next;
114+
115+
public Node() {}
116+
117+
public Node(int _val) {
118+
val = _val;
119+
}
120+
121+
public Node(int _val, Node _next) {
122+
val = _val;
123+
next = _next;
124+
}
125+
};
126+
*/
127+
128+
class Solution {
129+
public Node insert(Node head, int insertVal) {
130+
Node node = new Node(insertVal);
131+
if (head == null) {
132+
node.next = node;
133+
return node;
134+
}
135+
Node p = head;
136+
for (;;) {
137+
if (p.val <= insertVal && insertVal <= p.next.val ||
138+
p.val > p.next.val && (insertVal <= p.next.val || insertVal >= p.val) ||
139+
p.next == head) {
140+
node.next = p.next;
141+
p.next = node;
142+
break;
143+
}
144+
p = p.next;
145+
}
146+
return head;
147+
}
148+
}
80149
```
81150

82151
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public Node next;
6+
7+
public Node() {}
8+
9+
public Node(int _val) {
10+
val = _val;
11+
}
12+
13+
public Node(int _val, Node _next) {
14+
val = _val;
15+
next = _next;
16+
}
17+
};
18+
*/
19+
20+
class Solution {
21+
public Node insert(Node head, int insertVal) {
22+
Node node = new Node(insertVal);
23+
if (head == null) {
24+
node.next = node;
25+
return node;
26+
}
27+
Node p = head;
28+
for (;;) {
29+
if (p.val <= insertVal && insertVal <= p.next.val ||
30+
p.val > p.next.val && (insertVal <= p.next.val || insertVal >= p.val) ||
31+
p.next == head) {
32+
node.next = p.next;
33+
p.next = node;
34+
break;
35+
}
36+
p = p.next;
37+
}
38+
return head;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
# Definition for a Node.
3+
class Node:
4+
def __init__(self, val=None, next=None):
5+
self.val = val
6+
self.next = next
7+
"""
8+
9+
class Solution:
10+
def insert(self, head: 'Node', insertVal: int) -> 'Node':
11+
node = Node(insertVal)
12+
if head is None:
13+
node.next = node
14+
return node
15+
p = head
16+
while True:
17+
if p.val <= insertVal and insertVal <= p.next.val or \
18+
p.val > p.next.val and (insertVal <= p.next.val or insertVal >= p.val) or \
19+
p.next == head:
20+
21+
node.next = p.next
22+
p.next = node
23+
break
24+
25+
p = p.next
26+
return head

0 commit comments

Comments
 (0)