Skip to content
LC-0430 Medium LeetCode

430. Flatten a Multilevel Doubly Linked List

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 61% Topics: Linked List, Depth-First Search, Doubly-Linked List
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(n)
# Space: O(1)

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
        """
        curr = head
        while curr:
            if curr.child:
                curr_next = curr.next
                curr.child.prev = curr
                curr.next = curr.child
                last_child = curr
                while last_child.next:
                    last_child = last_child.next
                if curr_next:
                    last_child.next = curr_next
                    curr_next.prev = last_child
                curr.child = None
            curr = curr.next
        return head

Solution from kamyu104/LeetCode-Solutions · MIT