![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/18b9c553add1b91309b24825ee109668.png)
自己1:利用栈来保存上一次断掉的链表指针,这个地方参考了操作系统中中断机制的方法,感觉不错,用时超过96%,内存击败100%
"""
# Definition for a Node.
class Node(object):
def __init__(self, val, prev, next, child):
self.val = val
self.prev = prev
self.next = next
self.child = child
"""
class Solution(object):
def flatten(self, head):
"""
:type head: Node
:rtype: Node
"""
if not head:
return None
zhan_stack = list()
head_ = Node(-1,None,None,None)
res_pre = res_cur = head_
curr = head
while curr or len(zhan_stack) != 0:
if not curr and zhan_stack:
curr = zhan_stack.pop()
tmp = Node(curr.val,res_pre,res_cur.next,None)
res_cur.next = tmp
res_cur = res_cur.next
res_pre = res_pre.next
if curr.child:
if curr.next:
zhan_stack.append(curr.next)
curr = curr.child
else:
curr = curr.next
head_.next.prev = None
return head_.next