Back to writing

430. Flatten A Multilevel Doubly Linked List

430. Flatten a Multilevel Doubly Linked List

Medium


You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.

Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.

Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.

 

Example 1:

Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation: The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes:

Example 2:

Input: head = [1,2,null,3]
Output: [1,3,2]
Explanation: The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes:

Example 3:

Input: head = []
Output: []
Explanation: There could be empty list in the input.

 

Constraints:

  • The number of Nodes will not exceed 1000.
  • 1 <= Node.val <= 105

 

How the multilevel linked list is represented in test cases:

We use the multilevel linked list from Example 1 above:

 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

The serialization of each level is as follows:

[1,2,3,4,5,6,null]
[7,8,9,10,null]
[11,12,null]

To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:

[1,    2,    3, 4, 5, 6, null]
             |
[null, null, 7,    8, 9, 10, null]
                   |
[            null, 11, 12, null]

Merging the serialization of each level and removing trailing nulls we obtain:

[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]

 """
# 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: 'Optional[Node]') -> 'Optional[Node]':
        self.merge(head)
        return head

    def merge(self, head):

        node = head
        while node:
            if node.child:
                next_node = node.next 
                child = node.child
                node.child = None
                # update nodes
                node.next = child
                child.prev = node

                tail = self.merge(child)

                if next_node:
                    tail.next = next_node
                    next_node.prev = tail

            if node.next is None:
                return node

            node = node.next

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗞𝗲𝘆 𝗖𝗼𝗻𝗰𝗲𝗽𝘁𝘀:

  1. Scalability: https://lnkd.in/gpge_z76
  2. Latency vs Throughput: https://lnkd.in/g_amhAtN
  3. CAP Theorem: https://lnkd.in/g3hmVamx
  4. ACID Transactions: https://lnkd.in/gMe2JqaF
  5. Rate Limiting: https://lnkd.in/gWsTDR3m
  6. API Design: https://lnkd.in/ghYzrr8q
  7. Strong vs Eventual Consistency: https://lnkd.in/gJ-uXQXZ
  8. Distributed Tracing: https://lnkd.in/d6r5RdXG
  9. Sync vs Async Communication: https://lnkd.in/gC3F2nvr
  10. Batch vs Stream Processing: https://lnkd.in/g4_MzM4s
  11. Fault Tolerance: https://lnkd.in/dVJ6n3wA

𝗦𝘆𝘀𝘁𝗲𝗺 𝗗𝗲𝘀𝗶𝗴𝗻 𝗕𝘂𝗶𝗹𝗱𝗶𝗻𝗴 𝗕𝗹𝗼𝗰𝗸𝘀:

  1. Database: https://lnkd.in/gti8gjpz
  2. Horizontal vs Vertical Scaling: https://lnkd.in/gAH2e9du
  3. Caching: https://lnkd.in/gC9piQbJ
  4. Distributed Caching: https://lnkd.in/g7WKydNg
  5. Load Balancing: https://lnkd.in/gQaa8sXK
  6. SQL vs NoSQL: https://lnkd.in/g3WC_yxn
  7. Database Scaling: https://lnkd.in/gAXpSyWQ
  8. Data Replication: https://lnkd.in/gVAJxTpS
  9. Data Redundancy: https://lnkd.in/gNN7TF7n
  10. Database Sharding: https://lnkd.in/gMqqc6x9
  11. Database Indexes: https://lnkd.in/gCeshYVt
  12. Proxy Server: https://lnkd.in/gi8KnKS6
  13. WebSocket: https://lnkd.in/g76Gv2KQ
  14. API Gateway: https://lnkd.in/gnsJGJaM
  15. Message Queues: https://lnkd.in/gTzY6uk8

𝗔𝗿𝗰𝗵𝗶𝘁𝗲𝗰𝘁𝘂𝗿𝗮𝗹 𝗣𝗮𝘁𝘁𝗲𝗿𝗻𝘀:

  1. Event-Driven Architecture: https://lnkd.in/dp8CPvey
  2. Client-Server Architecture: https://lnkd.in/dAARQYzq
  3. Serverless Architecture: https://lnkd.in/gQNAXKkb
  4. Microservices Architecture: https://lnkd.in/gFXUrz_T

𝗟𝗼𝘄-𝗟𝗲𝘃𝗲𝗹 𝗗𝗲𝘀𝗶𝗴𝗻 𝗣𝗿𝗼𝗯𝗹𝗲𝗺𝘀:

  1. Design Parking Lot: https://lnkd.in/dQaAuFd2
  2. Design Splitwise: https://lnkd.in/dF5fBnex
  3. Design Chess Validator: https://lnkd.in/dfAQHvN4
  4. Design Distributed Queue | Kafka: https://lnkd.in/dQ6_B4_M

Share with someone who might need this.

FAANGTips