Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Flatten a Multilevel Doubly Linked List problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Flatten a Multilevel Doubly Linked List problem solution You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. 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.

Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.

Leetcode Flatten a Multilevel Doubly Linked List problem solution in Python.

class Solution:
    def flatten(self, head: 'Node') -> 'Node':
        
        def helper(head, stack=[]):
            if not head: return head
            
            node = head
            while node:
                if node.child:
                    if node.next: stack.append(node.next)
                    temp       = node.child
                    node.next  = temp
                    temp.prev  = node
                    node.child = None
                elif not node.next and stack:
                    temp = stack.pop()
                    node.next = temp
                    temp.prev = node
                node = node.next
            return head
        
        return helper(head)

Flatten a Multilevel Doubly Linked List problem solution in Java.

class Solution {
    public Node flatten(Node head) {
        
        Node refHead = head; 
        
        while (refHead != null) { 
            
            Node next = refHead.next; 
            
            Node child = flatten(refHead.child); 
            
            Node childRef = child;
            while (child != null && child.next != null) {
                
                child = child.next;
            }
            
            if (childRef != null) {
                refHead.child = null;
                refHead.next = childRef;
                childRef.prev = refHead;
                
                child.next = next;
                if (next != null) {
                
                    next.prev = child;
                }
            }
                        
            refHead = next;
        }
        
        return head;
    }
}

Problem solution in C++.

class Solution {
public:
    Node* flatten(Node* head) {
        flattenEnd(head);
        return head;
    }
    Node *flattenEnd(Node *head)
    {
        if (!head) return head;
        Node *fp = head, *next;
        while (fp)
        {
            next = fp->next;
            if (fp->child)
            {
                Node *end = flattenEnd(fp->child);
                fp->next = fp->child;
                fp->next->prev = fp;
                fp->child = NULL;
                fp = end;
                fp->next = next;
                if(next)
                    next->prev = end;
            }
            if (!next)
                break;
            fp = next;

        }
        return fp;
    }
};

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes