2
# Definition for a Node.4
def __init__(self, val, prev, next, child):13
def flatten(self, head: "Optional[Node]") -> "Optional[Node]":16
if node.child: # If there is a child travel to last node of the child22
) # Update the next of child to the the next of the current node25
): # update the prev of the next node to chile to make it valid doubly linked list26
node.next.prev = child29
) # Update the child to become the next of the current30
node.next.prev = node # update the prev of the next node to chile to make it valid doubly linked list31
node.child = None # Make the child of the current node None to fulfill the requirements36
# time and space complexity