Open In App

Multilevel Linked List

Last Updated : 25 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Multilevel Linked List
Multilevel Linked List is a 2D data structure that comprises several linked lists and each node in a multilevel linked list has a next and child pointer. All the elements are linked using pointers.

multilevel linked list

Representation:
A multilevel linked list is represented by a pointer to the first node of the linked lists. Similar to the linked list, the first node is called the head. If the multilevel linked list is empty, then the value of head is NULL. Each node in a list consists of at least three parts:
1. Data.
2. Pointer to the next node.
3. Pointer to the child node.

Each node of a multilevel linked list is represented as:

class Node
{
        public:
                 int data;
                 Node *next;
                 Node *child;
 };

Below is the implementation of the multilevel linked list

C++




// C++ program to implement
// a multilevel linked list
 
#include <bits/stdc++.h>
using namespace std;
 
// Representation of node
class Node {
public:
    int data;
    Node* next;
    Node* child;
};
 
// A function to create a linked list
// with n(size) nodes returns head pointer
Node* createList(int* arr, int n)
{
    Node* head = NULL;
    Node* tmp;
 
    // Traversing the passed array
    for (int i = 0; i < n; i++) {
        // Creating a node if the list
        // is empty
        if (head == NULL) {
            tmp = head = new Node();
        }
        else {
            tmp->next = new Node();
            tmp = tmp->next;
        }
        // Created a node with data and
        // setting child and next pointer
        // as NULL.
        tmp->data = arr[i];
        tmp->next = tmp->child = NULL;
    }
    return head;
}
 
// To print the linked list
void printMultiLevelList(Node* head)
{
    // While head is not null
    while (head) {
        if (head->child) {
            printMultiLevelList(head->child);
        }
        cout << head->data << " ";
        head = head->next;
    }
}
 
// Driver code
int main()
{
    // Initializing the data in arrays(row wise)
    int arr1[3] = { 1, 2, 3 };
    int arr2[2] = { 5, 6 };
    int arr3[1] = { 4 };
    int arr4[3] = { 7, 8, 9 };
 
    // creating Four linked lists
    // Passing array and size of array
    // as parameters
    Node* head1 = createList(arr1, 3);
    Node* head2 = createList(arr2, 2);
    Node* head3 = createList(arr3, 1);
    Node* head4 = createList(arr4, 3);
 
    // Initializing children and next pointers
    // as shown in given diagram
    head1->child = head2;
    head1->next->next->child = head3;
    head2->next->child = head4;
 
    // Creating a null pointer.
    Node* head = NULL;
    head = head1;
 
    // Function Call to print
    printMultiLevelList(head);
 
    return 0;
}


Java




// Java program to implement
// a multilevel linked list
 
 
public class GFG {
     
      // Representation of node
    public static class Node {
        public int data;
        public Node next;
        public Node child;
 
        public Node(int data) {
            this.data = data;
            this.next = null;
            this.child = null;
        }
    }
     
   
      // A function to create a linked list
    // with n(size) nodes returns head pointer
    public static Node createList(int[] arr, int n) {
        Node head = null;
        Node tmp = null;
         
      // Traversing the passed array
        for (int i = 0; i < n; i++) {
               // Creating a node if the list
                // is empty
            if (head == null) {
                tmp = head = new Node(arr[i]);
            } else {
                   
                  // Created a node with data and
                // setting child and next pointer
                // as NULL.
                tmp.next = new Node(arr[i]);
                tmp = tmp.next;
            }
        }
 
        return head;
    }
     
       
// To print the linked list
    public static void printMultiLevelList(Node head) {   
          // While head is not null
        while (head != null) {
            if (head.child != null) {
                printMultiLevelList(head.child);
            }
            System.out.print(head.data + " ");
            head = head.next;
        }
    }
 
    public static void main(String[] args) {
           
      // Initializing the data in arrays(row wise)
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {5, 6};
        int[] arr3 = {4};
        int[] arr4 = {7, 8, 9};
         
           // creating Four linked lists
        // Passing array and size of array
        // as parameters
        Node head1 = createList(arr1, 3);
        Node head2 = createList(arr2, 2);
        Node head3 = createList(arr3, 1);
        Node head4 = createList(arr4, 3);
         
          // Initializing children and next pointers
           // as shown in given diagram
        head1.child = head2;
        head1.next.next.child = head3;
        head2.next.child = head4;
         
          // Creating a null pointer.
        Node head = head1;
           
       
           // Function Call to print
        printMultiLevelList(head);
    }
}
 
// this code is contributed by bhardwajji


Python3




'''package whatever #do not write package name here '''
class Node:
    def __init__(self):
        self.data = 0;
        self.next = None;
        self.child = None;
 
# A function to create a linked list
# with n(size) Nodes returns head pointer
def createList(arr, n):
    head = None;
    tmp = None;
 
    # Traversing the passed array
    for i in range(n):
 
        # Creating a Node if the list
        # is empty
        if (head == None):
            tmp = head = Node();
        else:
            tmp.next = Node();
            tmp = tmp.next;
         
        # Created a Node with data and
        # setting child and next pointer
        # as NULL.
        tmp.data = arr[i];
        tmp.next = tmp.child = None;
     
    return head;
 
# To print linked list
def printMultiLevelList(head):
 
    # While head is not None
    while (head != None):
        if (head.child != None):
            printMultiLevelList(head.child);
         
        print(head.data, end=" ");
        head = head.next;
     
# Driver code
if __name__ == '__main__':
    arr1 = [ 1, 2, 3 ];
    arr2 = [ 5, 6] ;
    arr3 = [ 4 ];
    arr4 = [ 7, 8, 9] ;
 
    # creating Four linked lists
    # Passing array and size of array
    # as parameters
    head1 = createList(arr1, 3);
    head2 = createList(arr2, 2);
    head3 = createList(arr3, 1);
    head4 = createList(arr4, 3);
 
    # Initializing children and next pointers
    # as shown in given diagram
    head1.child = head2;
    head1.next.next.child = head3;
    head2.next.child = head4;
 
    # Creating a None pointer.
    head = None;
    head = head1;
 
    # Function Call to print
    printMultiLevelList(head);
 
# This code is contributed by umadevi9616


C#




/*package whatever //do not write package name here */
using System;
 
public class GFG {
   public class Node {
          public int data;
        public Node next;
        public Node child;
    };
 
    // A function to create a linked list
    // with n(size) nodes returns head pointer
    public static Node createList(int []arr, int n)
    {
        Node head = null;
        Node tmp = null;
 
        // Traversing the passed array
        for (int i = 0; i < n; i++)
        {
           
            // Creating a node if the list
            // is empty
            if (head == null) {
                tmp = head = new Node();
            }
            else {
                tmp.next = new Node();
                tmp = tmp.next;
            }
           
            // Created a node with data and
            // setting child and next pointer
            // as NULL.
            tmp.data = arr[i];
            tmp.next = tmp.child = null;
        }
        return head;
    }
 
    // To print the linked list
    public static void printMultiLevelList(Node head)
    {
       
        // While head is not null
        while (head != null) {
            if (head.child != null) {
                printMultiLevelList(head.child);
            }
            Console.Write(head.data + " ");
            head = head.next;
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr1 = { 1, 2, 3 };
        int []arr2 = { 5, 6 };
        int []arr3 = { 4 };
        int []arr4 = { 7, 8, 9 };
 
        // creating Four linked lists
        // Passing array and size of array
        // as parameters
        Node head1 = createList(arr1, 3);
        Node head2 = createList(arr2, 2);
        Node head3 = createList(arr3, 1);
        Node head4 = createList(arr4, 3);
 
        // Initializing children and next pointers
        // as shown in given diagram
        head1.child = head2;
        head1.next.next.child = head3;
        head2.next.child = head4;
 
        // Creating a null pointer.
        Node head = null;
        head = head1;
 
        // Function Call to print
        printMultiLevelList(head);
    }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
/*package whatever //do not write package name here */
    class Node {
        constructor(){
      this.data = 0;
        this.next = null;
        this.child = null;
        }
    }
 
    // A function to create a linked list
    // with n(size) nodes returns head pointer
    function createList(arr , n)
    {
        var head = null;
        var tmp = null;
 
        // Traversing the passed array
        for (var i = 0; i < n; i++)
        {
           
            // Creating a node if the list
            // is empty
            if (head == null) {
                tmp = head = new Node();
            }
            else {
                tmp.next = new Node();
                tmp = tmp.next;
            }
           
            // Created a node with data and
            // setting child and next pointer
            // as NULL.
            tmp.data = arr[i];
            tmp.next = tmp.child = null;
        }
        return head;
    }
 
    // To print the linked list
    function printMultiLevelList(head)
    {
       
        // While head is not null
        while (head != null) {
            if (head.child != null) {
                printMultiLevelList(head.child);
            }
            document.write(head.data + " ");
            head = head.next;
        }
    }
 
    // Driver code
 
        var arr1 = [ 1, 2, 3 ];
        var arr2 = [ 5, 6 ];
        var arr3 = [ 4 ];
        var arr4 = [ 7, 8, 9 ];
 
        // creating Four linked lists
        // Passing array and size of array
        // as parameters
        var head1 = createList(arr1, 3);
        var head2 = createList(arr2, 2);
        var head3 = createList(arr3, 1);
        var head4 = createList(arr4, 3);
 
        // Initializing children and next pointers
        // as shown in given diagram
        head1.child = head2;
        head1.next.next.child = head3;
        head2.next.child = head4;
 
        // Creating a null pointer.
        var head = null;
        head = head1;
 
        // Function Call to print
        printMultiLevelList(head);
 
// This code is contributed by Rajput-Ji
</script>


Output

5 7 8 9 6 1 2 4 3 


Similar Reads

Flatten a multilevel linked list
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 below figure. You are given the head of the first level of the list. Flatten the list so
15+ min read
C Program For Flattening A Multilevel Linked List
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 below figure.You are given the head of the first level of the list. Flatten the list so
5 min read
C++ Program For Flattening A Multilevel Linked List
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 below figure. You are given the head of the first level of the list. Flatten the list so th
5 min read
Java Program For Flattening A Multilevel Linked List
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 lis
5 min read
Python Program For Flattening A Multilevel Linked List
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 lis
4 min read
Difference between Singly linked list and Doubly linked list
Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node. Introduction to Doubly linked list : A Doubly Linked List (D
2 min read
Convert Singly Linked List to XOR Linked List
Prerequisite: XOR Linked List – A Memory Efficient Doubly Linked List | Set 1XOR Linked List – A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a singly linked list, the task is to convert the gi
9 min read
Create new linked list from two given linked list with greater element at each node
Given two linked list of the same size, the task is to create a new linked list using those linked lists. The condition is that the greater node among both linked list will be added to the new linked list.Examples: Input: list1 = 5-&gt;2-&gt;3-&gt;8 list2 = 1-&gt;7-&gt;4-&gt;5 Output: New list = 5-&gt;7-&gt;4-&gt;8 Input: list1 = 2-&gt;8-&gt;9-&gt;
8 min read
Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
Given a Linked List of even number of nodes, the task is to generate a new Linked List such that it contains the maximum difference of squares of node values in decreasing order by including each node in a single pair. Examples: Input: 1 -&gt; 6 -&gt; 4 -&gt; 3 -&gt; 5 -&gt;2Output: 35 -&gt; 21 -&gt; 7Explanation:The difference between squares of 6
11 min read
XOR linked list- Remove first node of the linked list
Given an XOR linked list, the task is to remove the first node of the XOR linked list. Examples: Input: XLL = 4 &lt; – &gt; 7 &lt; – &gt; 9 &lt; – &gt; 7 Output: 7 &lt; – &gt; 9 &lt; – &gt; 7 Explanation: Removing the first node of the XOR linked list modifies XLL to 7 &lt; – &gt; 9 &lt; – &gt; 7 Input: XLL = NULL Output: List Is Empty Approach: Th
11 min read
three90RightbarBannerImg