Skip to content
Related Articles

Related Articles

Java Program For Flattening A Multilevel Linked List

Improve Article
Save Article
Like Article
  • Last Updated : 22 Dec, 2021

Given a linked list where in addition to the next pointer, each node has a child pointer, which may or may not point to a separate 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 below figure. You are given the head of the first level of the list. Flatten the list so that all the nodes appear in a single-level linked list. You need to flatten the list in a way that all nodes at the first level should come first, then nodes of the second level, and so on.
Each node is a C struct with the following definition.


static class List
    public int data;
    public List next;
    public List child;
// This code is contributed by pratham76

The above list should be converted to 10->5->12->7->11->4->20->13->17->6->2->16->9->8->3->19->15

The problem clearly says that we need to flatten level by level. The idea of a solution is, we start from the first level, process all nodes one by one, if a node has a child, then we append the child at the end of the list, otherwise, we don’t do anything. After the first level is processed, all next-level nodes will be appended after the first level. The same process is followed for the appended nodes. 

1) Take the "cur" pointer, which will point to the head 
        of the first level of the list
2) Take the "tail" pointer, which will point to the end of 
   the first level of the list
3) Repeat the below procedure while "curr" is not NULL.
    I) If the current node has a child then
    a) Append this new child list to the "tail"
        tail->next = cur->child
    b) Find the last node of the new child list and update 
       the "tail"
        tmp = cur->child;
        while (tmp->next != NULL)
            tmp = tmp->next;
        tail = tmp;
    II) Move to the next node. i.e. cur = cur->next

Following is the implementation of the above algorithm. 


// Java program to flatten linked list with 
// next and child pointers
class LinkedList 
    static Node head;
    class Node 
        int data;
        Node next, child;
        Node(int d) 
            data = d;
            next = child = null;
    // A utility function to create a linked list 
    // with n nodes. The data of nodes is taken 
    // from arr[].  All child pointers are set 
    // as NULL
    Node createList(int arr[], int n) 
        Node node = null;
        Node p = null;
        int i;
        for (i = 0; i < n; ++i) 
            if (node == null
                node = p = new Node(arr[i]);
       = new Node(arr[i]);
                p =;
   = p.child = null;
        return node;
    // A utility function to print all 
    // nodes of a linked list
    void printList(Node node) 
        while (node != null
            System.out.print( + " ");
            node =;
    Node createList() 
        int arr1[] = new int[]{10, 5
                               12, 7, 11};
        int arr2[] = new int[]{4, 20, 13};
        int arr3[] = new int[]{17, 6};
        int arr4[] = new int[]{9, 8};
        int arr5[] = new int[]{19, 15};
        int arr6[] = new int[]{2};
        int arr7[] = new int[]{16};
        int arr8[] = new int[]{3};
        // Create 8 linked lists 
        Node head1 = createList(arr1,  
        Node head2 = createList(arr2, 
        Node head3 = createList(arr3, 
        Node head4 = createList(arr4, 
        Node head5 = createList(arr5, 
        Node head6 = createList(arr6, 
        Node head7 = createList(arr7, 
        Node head8 = createList(arr8, 
        // Modify child pointers to create 
        // the list shown above 
        head1.child = head2; = head3;
        head3.child = head4;
        head4.child = head5; = head6; = head7;
        head7.child = head8;
        /* Return head pointer of first linked list.  
           Note that all nodes are reachable from 
           head1 */
        return head1;
    /* The main function that flattens a multilevel 
       linked list */
    void flattenList(Node node) 
        // Base case
        if (node == null
        Node tmp = null;
        /* Find tail node of first level 
           linked list */
        Node tail = node;
        while ( != null
            tail =;
        // One by one traverse through all nodes 
        // of first level linked list till we 
        // reach the tail node
        Node cur = node;
        while (cur != tail) 
            // If current node has a child
            if (cur.child != null
                // Then append the child at the 
                // end of current list
       = cur.child;
                // And update the tail to new 
                // last node
                tmp = cur.child;
                while ( != null
                    tmp =;
                tail = tmp;
            // Change current node
            cur =;
    // Driver code
    public static void main(String[] args) 
        LinkedList list = new LinkedList();
        head = list.createList();
// This code has been contributed by Mayank Jaiswal


10 5 12 7 11 4 20 13 17 6 2 16 9 8 3 19 15

Time Complexity: Since every node is visited at most twice, the time complexity is O(n) where n is the number of nodes in given linked list.

Please refer complete article on Flatten a multilevel linked list for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!