Leetcode 766 Solution
This article provides solution to leetcode question 766 (flatten-a-multilevel-doubly-linked-list)
Access this page by simply typing in "lcs 766" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list
Solution
"""
# 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):
if not head:
return None
s = []
s.append(head)
prev = None
while s:
node = s.pop(-1)
if node.next:
s.append(node.next)
if node.child:
s.append(node.child)
node.prev = prev
if prev:
prev.next = node
node.child = None
node.next = None
prev = node
return head