Flatten a multilevel doubly linked list, splicing each child list after its parent node

Quick Overview

Given a doubly linked list whose nodes can point to child lists nested to any depth, rewire the pointers so every node sits in one flat doubly linked list, with each child list placed right after its parent. It tests linked list pointer surgery, depth-first ordering and careful handling of prev and child pointers.

Flatten a multilevel doubly linked list, splicing each child list after its parent node

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a doubly linked list whose nodes have a `next` pointer, a `prev` pointer and an extra `child` pointer. A `child` pointer is either `None` or points to the head of a separate doubly linked list made of the same kind of node. Those child lists may have children of their own, and so on, which produces a multilevel data structure. Given the `head` of the first level, flatten the structure so that all the nodes appear in a single-level doubly linked list. When a node `curr` has a child list, the nodes of that child list (flattened the same way) appear after `curr` and before the node that was `curr.next`. Return the `head` of the flattened list. Every node in the result must have its `child` pointer set to `None`. ### Function Signature ```python def flatten(head: Optional[Node]) -> Optional[Node]: ``` ### Rules Nodes use this class: ```python class Node: def __init__(self, val, prev=None, next=None, child=None): self.val = val self.prev = prev self.next = next self.child = child ``` - Before flattening, the head of every child list has `prev` set to `None`. - Example inputs are written as nested lists. An integer is a node without a child. A two-element list `[val, sublist]` is a node with value `val` whose `child` points to the list written as `sublist`, which uses the same notation and is never empty. `[]` means `head` is `None`. - The output is written as the node values from head to tail. The result must also be a valid doubly linked list: `head.prev` is `None`, every other node's `prev` is the node before it, the tail's `next` is `None`, and every `child` is `None`. ### Constraints - The structure holds at most 1000 nodes across all levels, and `head` may be `None`. - `1 <= Node.val <= 10^5` ### Examples ```text Input: head = [1, 2, [3, [7, [8, [11, 12]], 9, 10]], 4, 5, 6] Output: [1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6] ``` The first level is 1-2-3-4-5-6, node 3 has the child list 7-8-9-10, and node 8 has the child list 11-12. The list 11-12 goes between 8 and 9, and the whole block from 7 to 10 (now holding 11 and 12) goes between 3 and 4. ```text Input: head = [1, 2] Output: [1, 2] ``` No node has a child, so the list is already flat and comes back unchanged. ```text Input: head = [[1, [7, 8]], 2, [3, [9]]] Output: [1, 7, 8, 2, 3, 9] ``` Node 1's child list 7-8 goes between 1 and 2. Node 3 is the last node of its level, so its child 9 becomes the new tail.

Overview: Given a doubly linked list whose nodes can point to child lists nested to any depth, rewire the pointers so every node sits in one flat doubly linked list, with each child list placed right after its parent. It tests linked list pointer surgery, depth-first ordering and careful handling of prev and child pointers.

General logo
General
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given a doubly linked list whose nodes have a next pointer, a prev pointer and an extra child pointer. A child pointer is either None or points to the head of a separate doubly linked list made of the same kind of node. Those child lists may have children of their own, and so on, which produces a multilevel data structure.

Given the head of the first level, flatten the structure so that all the nodes appear in a single-level doubly linked list. When a node curr has a child list, the nodes of that child list (flattened the same way) appear after curr and before the node that was curr.next.

Return the head of the flattened list. Every node in the result must have its child pointer set to None.

Function Signature

def flatten(head: Optional[Node]) -> Optional[Node]:

Rules

Nodes use this class:

class Node:
    def __init__(self, val, prev=None, next=None, child=None):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child
  • Before flattening, the head of every child list has prev set to None .
  • Example inputs are written as nested lists. An integer is a node without a child. A two-element list [val, sublist] is a node with value val whose child points to the list written as sublist , which uses the same notation and is never empty. [] means head is None .
  • The output is written as the node values from head to tail. The result must also be a valid doubly linked list: head.prev is None , every other node's prev is the node before it, the tail's next is None , and every child is None .

Constraints

  • The structure holds at most 1000 nodes across all levels, and head may be None .
  • 1 <= Node.val <= 10^5

Examples

Input: head = [1, 2, [3, [7, [8, [11, 12]], 9, 10]], 4, 5, 6]
Output: [1, 2, 3, 7, 8, 11, 12, 9, 10, 4, 5, 6]

The first level is 1-2-3-4-5-6, node 3 has the child list 7-8-9-10, and node 8 has the child list 11-12. The list 11-12 goes between 8 and 9, and the whole block from 7 to 10 (now holding 11 and 12) goes between 3 and 4.

Input: head = [1, 2]
Output: [1, 2]

No node has a child, so the list is already flat and comes back unchanged.

Input: head = [[1, [7, 8]], 2, [3, [9]]]
Output: [1, 7, 8, 2, 3, 9]

Node 1's child list 7-8 goes between 1 and 2. Node 3 is the last node of its level, so its child 9 becomes the new tail.