430. 扁平化多级双向链表
为保证权益,题目请参考 430. 扁平化多级双向链表(From LeetCode).
解决方案1
Python
python
# 430. 扁平化多级双向链表
# https://leetcode-cn.com/problems/flatten-a-multilevel-doubly-linked-list/
################################################################################
# Definition for a Node.
class Node:
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
class Solution:
def flatten(self, head: "Node") -> "Node":
if head is None:
return None
t = head
while t is not None:
if t.child is not None:
t2 = t.next
t3 = self.flatten(t.child)
t.next = t3
t.child = None
t3.prev = t
while t3.next is not None:
t3 = t3.next
t3.next = t2
if t2 is not None:
t2.prev = t3
t = t.next
return head
################################################################################
if __name__ == "__main__":
solution = Solution()
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
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