Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Flatten a Multilevel Doubly Linked List problem solution

YASH PAL, 31 July 2024

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.

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)

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

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes