Linked List of Linked List
Last Updated :
11 May, 2024
Linked list of linked list, also known as nested linked lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage complex data. In this article we will learn about the basics of Linked List of Linked List, how to create Linked List of Linked List, its applications, advantages and disadvantages.

Representation of Linked List of Linked List:
A Linked List of 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:
- Data.
- Pointer to the next node.
- Pointer to the child node.
Each node of a multilevel linked list is represented as:
class Node
{
public:
int data;
Node *next;
Node *child;
};
Creating a Linked List of Linked Lists:
Below is the implementation of the multilevel linked list
C++
#include <iostream>
#include <vector>
using namespace std;
// Class to represent the node of the linked list
class Node {
public:
int data;
Node* next;
Node* child;
Node()
{
data = 0;
next = nullptr;
child = nullptr;
}
};
// A function to create a linked list
// with n(size) Nodes returns head pointer
Node* createList(vector<int>& arr, int n)
{
Node* head = nullptr;
Node* tmp = nullptr;
// Traversing the passed array
for (int i = 0; i < n; i++) {
// Creating a Node if the list
// is empty
if (head == nullptr) {
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 = nullptr;
}
return head;
}
// To print linked list
void printMultiLevelList(Node* head)
{
// While head is not None
while (head != nullptr) {
if (head->child != nullptr) {
printMultiLevelList(head->child);
}
cout << head->data << " ";
head = head->next;
}
}
// Driver code
int main()
{
vector<int> arr1 = { 1, 2, 3 };
vector<int> arr2 = { 5, 6 };
vector<int> arr3 = { 4 };
vector<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 None pointer.
Node* head = nullptr;
head = head1;
// Function Call to print
printMultiLevelList(head);
// Clean up
delete head1;
delete head2;
delete head3;
delete head4;
return 0;
}
// This code is contributed by Prachi.
Java
class Node {
int data;
Node next;
Node child;
Node(int data) {
this.data = data;
this.next = null;
this.child = null;
}
}
public class Main {
// A function to create a linked list with n (size) Nodes, returns head pointer
static Node createList(int[] arr) {
Node head = null;
Node tmp = null;
// Traversing the passed array
for (int i = 0; i < arr.length; i++) {
// Creating a Node if the list is empty
if (head == null) {
tmp = head = new Node(arr[i]);
} else {
tmp.next = new Node(arr[i]);
tmp = tmp.next;
}
// Created a Node with data and setting child and next pointer as NULL.
tmp.child = null;
}
return head;
}
// To print linked list
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;
}
}
// 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);
Node head2 = createList(arr2);
Node head3 = createList(arr3);
Node head4 = createList(arr4);
// 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);
// Clean up (Java does manual memory management, no cleanup needed in this case)
}
}
Python
# Class to represent the node of the linked list
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)
JavaScript
// Class to represent the node of the linked list
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) {
let head = null;
let tmp = null;
// Traversing the passed array
for (let i = 0; i < arr.length; 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 linked list
function printMultiLevelList(head) {
// While head is not null
while (head !== null) {
if (head.child !== null) {
printMultiLevelList(head.child);
}
console.log(head.data + " ");
head = head.next;
}
}
// Driver code
function main() {
let arr1 = [1, 2, 3];
let arr2 = [5, 6];
let arr3 = [4];
let arr4 = [7, 8, 9];
// creating Four linked lists
// Passing array and size of array as parameters
let head1 = createList(arr1);
let head2 = createList(arr2);
let head3 = createList(arr3);
let head4 = createList(arr4);
// 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.
let head = null;
head = head1;
// Function Call to print
printMultiLevelList(head);
// Clean up (JavaScript handles memory management automatically)
}
// Call the main function
main();
Applications of Linked List of Linked List:
- Hierarchical Data Representation: Lists that are linked to other lists form structures for data that has hierarchies. This is useful for folders on computers, business roles, or categories inside categories.
- Sparse Matrix Representation: Sparse matrices are very common in the field of computer science. They often get stored and picked up using lists of lists that are linked together. This way of storing allows for dealing with parts of the matrix that are not zero well. Sparse matrices have many zeros in them so a linked list setup works great.
- Multi-level Data Structures: Some algorithms and data structures, such as skip lists or multi-level caching systems, can benefit from the flexibility and dynamic nature of linked lists of linked lists.
Advantages of Linked List of Linked List:
- Flexibility: Lists connected to other lists are great for complex records with changing nested levels. Some are simple. Yet, others are convoluted, with deep layers.
- Dynamic Size: Unlike arrays or matrices, linked lists of linked lists can dynamically adjust their size without requiring pre-allocation of memory.
- Efficient Insertions and Deletions: Insertions and deletions within a linked list of linked lists can be more efficient compared to arrays or matrices, especially when dealing with large data sets.
Disadvantages of Linked List of Linked List:
- Increased Memory Overhead: Maintaining pointers or references between nodes can result in increased memory overhead compared to contiguous data structures like arrays.
- Slower Access Time: Accessing elements in a linked list of linked lists typically requires traversing multiple levels, which can result in slower access times compared to direct indexing in arrays or matrices.
- Complexity: Managing nested data structures introduces complexity. This is true for linked list concepts, which are unfamiliar to some developers. Implementation and understanding become challenging with nested structures.
Conclusion:
Linked lists of linked lists provides a way to sort data by levels. This approach has upsides, like being able to grow or shrink easy. But it has some downsides too. It needs more memory space than other data types. And finding items inside takes more time. It's key to understand the trade-offs before using linked lists of linked lists in projects.
Similar Reads
Introduction to Multi Linked List A multi-linked list is a special type of list that contains two or more logical key sequences. Before checking details about multi-linked list, see what is a linked list. A linked list is a data structure that is free from any size restriction until the heap memory is not full. We have seen differen
6 min read
Linked List in C++ In C++, a linked list is a linear data structure that allows the users to store data in non-contiguous memory locations. A linked list is defined as a collection of nodes where each node consists of two members which represents its value and a next/previous pointer which stores the address for the n
6 min read
Stack Using Linked List in C Stack is a linear data structure that follows the Last-In-First-Out (LIFO) order of operations. This means the last element added to the stack will be the first one to be removed. There are different ways using which we can implement stack data structure in C. In this article, we will learn how to i
7 min read
Doubly Linked List in Python Doubly Linked List is a type of linked list in which each node contains a data element and two links pointing to the next and previous node in the sequence. This allows for more efficient operations such as traversals, insertions, and deletions because it can be done in both directions. Table of Con
13 min read
Print Linked List Given a Singly Linked List, the task is to print all the elements in the list.Examples:Input: 1->2->3->4->5->nullOutput: 1 2 3 4 5Explanation: Every element of each node from head node to last node is printed.Input: 10->20->30->40->50->nullOutput: 10 20 30 40 50Explanat
9 min read
Delete alternate nodes of a Linked List Given a Singly Linked List, starting from the second node delete all alternate nodes of it. For example, if the given linked list is 1->2->3->4->5 then your function should convert it to 1->3->5, and if the given linked list is 1->2->3->4 then convert it to 1->3. Recomm
14 min read
Generalized Linked List A Generalized Linked List L, is defined as a finite sequence of n>=0 elements, l1, l2, l3, l4, ..., ln, such that li are either item or the list of items. Thus L = (l1, l2, l3, l4, ..., ln) where n is total number of nodes in the list. Â To represent a list of items there are certain assumptions a
10 min read
Header Linked List A header linked list is a special type of linked list which consists of a header node in the beginning of the list. The header node can store meta data about the linked list. This type of list is useful when information other than that found in each node is needed. For example, suppose there is an a
11 min read
Linked List meaning in DSA A linked list is a linear data structure used for storing a sequence of elements, where each element is stored in a node that contains both the element and a pointer to the next node in the sequence. Linked ListTypes of linked lists: Linked lists can be classified in the following categories Singly
4 min read
Linked List Data Structure A linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
3 min read