Skip to content

Commit fdc9cad

Browse files
committed
2019-12-29 430. Flatten a Multilevel Doubly Linked List
1 parent 8e6245d commit fdc9cad

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
# @Author: 何睿
3+
# @Create Date: 2019-12-29 10:57:03
4+
# @Last Modified by: 何睿
5+
# @Last Modified time: 2019-12-29 11:05:17
6+
7+
8+
class Node:
9+
def __init__(self, val, prev, next, child):
10+
self.val = val
11+
self.prev = prev
12+
self.next = next
13+
self.child = child
14+
15+
16+
class Solution:
17+
def flatten(self, head: 'Node') -> 'Node':
18+
current = head
19+
while current:
20+
if current.child:
21+
_next = current.next # 当前节点的后一个节点
22+
last = current.child # 当前节点的自节点
23+
while last.next:
24+
last = last.next # 如果有子节点,找到子节点的最后一个节点
25+
current.next = current.child
26+
current.next.prev = current # 将自节点向上提高一层
27+
current.child = None
28+
last.next = _next
29+
if _next:
30+
_next.prev = last
31+
current = current.next
32+
33+
return head

0 commit comments

Comments
 (0)