A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.
Types Of Linked Lists:
1. Singly Linked List
Singly linked list is the simplest type of linked list in which every node contains some data and a pointer to the next node of the same data type.
The node contains a pointer to the next node means that the node stores the address of the next node in the sequence. A single linked list allows the traversal of data only in one way. Below is the image for the same:
Below is the structure of the singly linked list:
C++
// Definition of a Node in a singly linked list
struct Node {
// Data part of the node
int data;
// Pointer to the next node in the list
Node* next;
// Constructor to initialize the node with data
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
// Data part of the node
int data;
// Pointer to the next node in the list
struct Node* next;
};
// Function to create a new node with given data
struct Node* newNode(int data) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
Java
// Definition of a Node in a singly linked list
public class Node {
int data;
Node next;
// Constructor to initialize the node with data
public Node(int data) {
this.data = data;
this.next = null;
}
}
Python
# Definition of a Node in a singly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
C#
// Definition of a Node in a singly linked list
public class Node {
int data;
Node next;
// Constructor to initialize the node with data
public Node(int data) {
this.data = data;
this.next = null;
}
}
JavaScript
// Definition of a Node in a singly linked list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
Below is the creation and traversal of Singly Linked List:
C++
// C++ program to illustrate creation
// and traversal of Singly Linked List
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int data) {
this->data = data;
this->next = nullptr;
}
};
void printList(Node* curr) {
// Iterate till n reaches NULL
while ( curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
}
int main() {
//Linked List 1 -> 2 -> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->next = third;
printList(head);
return 0;
}
C
// C program to illustrate creation
// and traversal of Singly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* node =
(struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
void printList(struct Node* n) {
// Iterate till n reaches NULL
while (n != NULL) {
printf("%d ", n->data);
n = n->next;
}
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
//Linked List 1 -> 2 -> 3
head = createNode(1);
second = createNode(2);
third = createNode(3);
head->next = second;
second->next = third;
printList(head);
return 0;
}
Java
// Java program to illustrate creation
// and traversal of Singly Linked List
class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public class GfG {
public static void printList(Node n) {
// Iterate till n reaches null
while (n != null) {
// Print the data
System.out.print(n.data + " ");
n = n.next;
}
}
public static void main(String[] args) {
//Linked List 1 -> 2 -> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.next = third;
printList(head);
}
}
Python
# Python program to illustrate creation
# and traversal of Singly Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
def print_list(node):
# Iterate till node reaches None
while node is not None:
# Print the data
print(node.data, end=" ")
node = node.next
if __name__ == "__main__":
#Linked List 1 -> 2 -> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.next = third
print_list(head)
C#
// C# program to illustrate creation
// and traversal of Singly Linked List
using System;
class Node {
public int data;
public Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
class GfG {
static void PrintList(Node node) {
// Iterate till node reaches null
while (node != null) {
Console.Write(node.data + " ");
node = node.next;
}
}
static void Main() {
//Linked List 1 -> 2 -> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.next = third;
PrintList(head);
}
}
JavaScript
// Javascript program to illustrate creation
// and traversal of Singly Linked List
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function printList(n) {
// Iterate till n reaches null
while (n != null) {
// Print the data
console.log(n.data);
n = n.next;
}
}
//Linked List 1 -> 2 -> 3
let head = new Node(1);
let second = new Node(2);
let third = new Node(3);
head.next = second;
second.next = third;
printList(head);
Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space: O(1)
2. Doubly Linked List
A doubly linked list or a two-way linked list is a more complex type of linked list that contains a pointer to the next as well as the previous node in sequence.
Therefore, it contains three parts of data, a pointer to the next node, and a pointer to the previous node. This would enable us to traverse the list in the backward direction as well.
Below is the structure of the doubly linked list :
C++
// Define the Node structure
struct Node {
int data;
Node* next;
Node* prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
C
// Define the Node structure
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
Java
// Define the Node class
class Node {
int data;
Node next;
Node prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Python
# Define the Node class
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
C#
// Define the Node class
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int data) {
Data = data;
Next = null;
Prev = null;
}
}
JavaScript
// Define the Node class
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
Below is the creation and traversal of doubly linked list:
C++
// C++ program to illustrate creation
// and traversal of doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
void forwardTraversal(Node* head) {
Node* curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
void backwardTraversal(Node* tail) {
Node* curr = tail;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->prev;
}
cout << endl;
}
int main() {
//Linked List 1 <-> 2 <-> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Forward Traversal:" << endl;
forwardTraversal(head);
cout << "Backward Traversal:" << endl;
backwardTraversal(third);
return 0;
}
C
// C program to illustrate creation
// and traversal of doubly Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void forwardTraversal(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
void backwardTraversal(struct Node* tail) {
struct Node* curr = tail;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->prev;
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
//Linked List 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Forward Traversal:\n");
forwardTraversal(head);
printf("Backward Traversal:\n");
backwardTraversal(third);
return 0;
}
Java
// Java program to illustrate creation
// and traversal of doubly Linked List
class Node {
int data;
Node next;
Node prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class GfG {
static void forwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
static void backwardTraversal(Node tail) {
Node curr = tail;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.prev;
}
System.out.println();
}
public static void main(String[] args) {
//Linked List 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.println("Forward Traversal:");
forwardTraversal(head);
System.out.println("Backward Traversal:");
backwardTraversal(third);
}
}
Python
# Python program to illustrate creation
# and traversal of doubly Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def forward_traversal(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
def backward_traversal(tail):
curr = tail
while curr is not None:
print(curr.data, end=" ")
curr = curr.prev
print()
if __name__ == "__main__":
#Linked List 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Forward Traversal:")
forward_traversal(head)
print("Backward Traversal:")
backward_traversal(third)
C#
// C# program to illustrate creation
// and traversal of doubly Linked List
using System;
class Node {
public int Data;
public Node next;
public Node prev;
public Node(int data) {
Data = data;
next = null;
prev = null;
}
}
class GfG {
static void ForwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void BackwardTraversal(Node tail) {
Node curr = tail;
while (curr != null) {
Console.Write(curr.Data + " ");
curr = curr.prev;
}
Console.WriteLine();
}
static void Main() {
//Linked List 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
Console.WriteLine("Forward Traversal:");
ForwardTraversal(head);
Console.WriteLine("Backward Traversal:");
BackwardTraversal(third);
}
}
JavaScript
// Javascript program to illustrate creation
// and traversal of doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
function forwardTraversal(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
function backwardTraversal(tail) {
let curr = tail;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.prev;
}
console.log();
}
//Linked List 1 <-> 2 <-> 3
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Forward Traversal:");
forwardTraversal(head);
console.log("Backward Traversal:");
backwardTraversal(third);
OutputForward Traversal:
1 2 3
Backward Traversal:
3 2 1
Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space: O(1)
3. Circular Linked List
A circular linked list is a type of linked list in which the last node's next pointer points back to the first node of the list, creating a circular structure. This design allows for continuous traversal of the list, as there is no null to end the list.
While traversing a circular linked list, we can begin at any node and traverse the list in any direction forward and backward until we reach the same node we started. Thus, a circular linked list has no beginning and no end. Below is the image for the same:
Below is the structure of the circular linked list :
C++
// Define the Node structure
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
C
// Define the Node structure
struct Node {
int data;
struct Node *next;
};
struct Node *createNode(int value) {
struct Node *newNode =
(struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
Java
// Define the Node structure
class Node {
int data;
Node next;
Node(int value){
data = value;
next = null;
}
}
Python
# Define the Node structure
class Node:
def __init__(self, data):
self.data = data
self.next = None
C#
// Define the Node structure
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}
JavaScript
// Define the Node structure
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
Below is the creation and traversal of circular linked list:
C++
// C++ program to illustrate creation
// and traversal of Circular Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
void printList(Node* last){
if(last == NULL) return;
// Start from the head node
Node* head = last->next;
while (true) {
cout << head->data << " ";
head = head->next;
if (head == last->next)
break;
}
cout << endl;
}
int main(){
// Create circular linked list: 2, 3, 4
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
printList(last);
return 0;
}
C
// C program to illustrate creation
// and traversal of Circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void printList(struct Node *last) {
if (last == NULL) return;
struct Node *head = last->next;
while (1){
printf("%d ", head->data);
head = head->next;
if (head == last->next)
break;
}
printf("\n");
}
struct Node *createNode(int value) {
struct Node *newNode =
(struct Node *)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int main() {
// Create circular linked list: 2, 3, 4
struct Node *first = createNode(2);
first->next = createNode(3);
first->next->next = createNode(4);
struct Node *last = first->next->next;
last->next = first;
printList(last);
return 0;
}
Java
// Java program to illustrate creation
// and traversal of Circular Linked List
class Node {
int data;
Node next;
Node(int value){
data = value;
next = null;
}
}
class GFG {
static void printList(Node last){
if (last == null)
return;
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next) break;
}
System.out.println();
}
public static void main(String[] args){
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
printList(last);
}
}
Python
# Python program to illustrate creation
# and traversal of Circular Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
def print_list(last):
if last is None:
return
head = last.next
while True:
print(head.data, end=" ")
head = head.next
if head == last.next:
break
print()
# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
print_list(last)
C#
// C# program to illustrate creation
// and traversal of Circular Linked List
using System;
class Node {
public int data;
public Node next;
public Node(int value) {
data = value;
next = null;
}
}
class GfG {
static void PrintList(Node last) {
if (last == null)
return;
Node head = last.next;
while (true) {
Console.Write(head.data + " ");
head = head.next;
if (head == last.next)
break;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
PrintList(last);
}
}
JavaScript
// Javascript program to illustrate creation
// and traversal of Circular Linked List
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function printList(last) {
if (last === null)
return;
let head = last.next;
while (true) {
console.log(head.data + " ");
head = head.next;
if (head === last.next)
break;
}
console.log();
}
// Create circular linked list: 2, 3, 4
const first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
printList(last);
Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space: O(1)
4. Doubly Circular linked list
Doubly Circular linked list or a circular two-way linked list is a complex type of linked list that contains a pointer to the next as well as the previous node in the sequence. The difference between the doubly linked and circular doubly list is the same as that between a singly linked list and a circular linked list. The circular doubly linked list does not contain null in the previous field of the first node.
Below is the structure of the Doubly Circular Linked List:
C++
// Structure of a Node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
Java
// Node class for Circular Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int data) {
this.data = data;
this.next = this;
this.prev = this;
}
}
Python
# structure of Node
class Node:
def __init__(self, data):
self.previous = None
self.data = data
self.next = None
C#
// Define the Node class
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int data) {
Data = data;
Next = null;
Prev = null;
}
}
JavaScript
// Structure of a Node
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
Below is the creation and traversal of doubly circular linked list:
C++
// C++ program to illustrate creation
// and traversal of doubly circular Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
void forwardTraversal(Node* head) {
Node* curr = head;
do {
cout << curr->data << " ";
curr = curr->next;
} while (curr != head);
cout << endl;
}
void backwardTraversal(Node* head) {
Node* curr = head->prev;
do {
cout << curr->data << " ";
curr = curr->prev;
} while (curr->next != head);
cout << endl;
}
int main() {
// Create a doubly circular linked list
// 1 <-> 2 <-> 3 <-> 1
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
third->next = head;
head->prev = third;
cout << "Forward Traversal:" << endl;
forwardTraversal(head);
cout << "Backward Traversal:" << endl;
backwardTraversal(head);
return 0;
}
C
// C program to illustrate creation
// and traversal of doubly circular Linked List
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void forwardTraversal(struct Node* head) {
struct Node* curr = head;
if (head != NULL) {
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
}
printf("\n");
}
void backwardTraversal(struct Node* head) {
struct Node* curr = head->prev;
if (head != NULL) {
do {
printf("%d ", curr->data);
curr = curr->prev;
} while (curr->next != head);
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Create a doubly circular linked list
// 1 <-> 2 <-> 3 <-> 1
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
// Make it circular
third->next = head;
head->prev = third;
printf("Forward Traversal:\n");
forwardTraversal(head);
printf("Backward Traversal:\n");
backwardTraversal(head);
return 0;
}
Java
// Java program to illustrate creation
// and traversal of doubly circular Linked List
class Node {
int data;
Node next;
Node prev;
public Node(int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class GfG {
static void forwardTraversal(Node head) {
Node curr = head;
if (head != null) {
do {
System.out.print(curr.data + " ");
curr = curr.next;
} while (curr != head);
}
System.out.println();
}
static void backwardTraversal(Node head) {
Node curr = head.prev;
if (head != null) {
do {
System.out.print(curr.data + " ");
curr = curr.prev;
} while (curr.next != head);
}
System.out.println();
}
public static void main(String[] args) {
// Create a doubly circular linked list
// 1 <-> 2 <-> 3 <-> 1
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
third.next = head;
head.prev = third;
System.out.println("Forward Traversal:");
forwardTraversal(head);
System.out.println("Backward Traversal:");
backwardTraversal(head);
}
}
Python
# Python program to illustrate creation
# and traversal of doubly circular Linked List
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
def forward_traversal(head):
curr = head
if head is not None:
while True:
print(curr.data, end=" ")
curr = curr.next
if curr == head:
break
print()
def backward_traversal(head):
curr = head.prev
if head is not None:
while True:
print(curr.data, end=" ")
curr = curr.prev
if curr.next == head:
break
print()
if __name__ == "__main__":
# Create a doubly circular linked list
# 1 <-> 2 <-> 3 <-> 1
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
third.next = head
head.prev = third
print("Forward Traversal:")
forward_traversal(head)
print("Backward Traversal:")
backward_traversal(head)
C#
// C# program to illustrate creation
// and traversal of doubly circular Linked List
using System;
class Node {
public int Data;
public Node next;
public Node prev;
public Node(int data) {
Data = data;
next = null;
prev = null;
}
}
class GfG {
static void ForwardTraversal(Node head) {
if (head == null) return;
Node curr = head;
do {
Console.Write(curr.Data + " ");
curr = curr.next;
} while (curr != head);
Console.WriteLine();
}
static void BackwardTraversal(Node head) {
if (head == null) return;
Node curr = head.prev;
do {
Console.Write(curr.Data + " ");
curr = curr.prev;
} while (curr.next != head);
Console.WriteLine();
}
static void Main() {
// Create a doubly circular linked list
// 1 <-> 2 <-> 3 <-> 1
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
third.next = head;
head.prev = third;
Console.WriteLine("Forward Traversal:");
ForwardTraversal(head);
Console.WriteLine("Backward Traversal:");
BackwardTraversal(head);
}
}
JavaScript
// Javascript program to illustrate creation
// and traversal of doubly circular Linked List
class Node {
constructor(data) {
this.data = data;
this.prev = null;
this.next = null;
}
}
function forwardTraversal(head) {
if (head === null) return;
let curr = head;
do {
console.log(curr.data + " ");
curr = curr.next;
} while (curr !== head);
console.log();
}
function backwardTraversal(head) {
if (head === null) return;
let curr = head.prev;
do {
console.log(curr.data + " ");
curr = curr.prev;
} while (curr.next !== head);
console.log();
}
// Create a doubly circular linked list
// 1 <-> 2 <-> 3 <-> 1
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
third.next = head;
head.prev = third;
console.log("Forward Traversal:");
forwardTraversal(head);
console.log("Backward Traversal:");
backwardTraversal(head);
OutputForward Traversal:
1 2 3
Backward Traversal:
3 2 1
Time Complexity: O(n), where n is the number of Nodes.
Auxiliary space: O(1)
5. Multilevel Linked List :
A multilevel linked list is an advanced data structure designed to represent hierarchical relationships among elements. Each node in this structure contains three main components: data, a next pointer, and a child pointer. The next pointer links to the next node in the same level of the list, allowing for linear traversal within that level. The child pointer, on the other hand, points to a sub-list or nested linked list, creating a hierarchical or tree-like structure. This enables each node to have its own sub-list of nodes, allowing for complex nested relationships. Please refer to Multi Linked List for Implementation.
6. Skip-List :
A skip list is a data structure that allows for efficient search, insertion and deletion of elements in a sorted list. It is a probabilistic data structure, meaning that its average time complexity is determined through a probabilistic analysis. Skip lists are implemented using a technique called “coin flipping.” In this technique, a random number is generated for each insertion to determine the number of layers the new element will occupy. This means that, on average, each element will be in log(n) layers, where n is the number of elements in the bottom layer. Please refer to Introduction of Skip List.
Note: Header Linked Lists are not a distinct type of linked list but rather a technique used within existing linked list structures, such as singly or doubly linked lists. This technique involves using a dummy node, also known as a header node, to simplify operations like insertion, deletion, and traversal. Please refer to Header Linked Lists for implementation.
Similar Reads
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
Basic Terminologies of Linked List Linked List is a linear data structure, in which elements are not stored at a contiguous location, rather they are linked using pointers. Linked List forms a series of connected nodes, where each node stores the data and the address of the next node.Node Structure: A node in a linked list typically
2 min read
Introduction to Linked List - Data Structure and Algorithm Tutorials Linked List is basically chains of nodes where each node contains information such as data and a pointer to the next node in the chain. It is a popular data structure with a wide range of real-world applications. Unlike Arrays, Linked List elements are not stored at a contiguous location. In the lin
9 min read
Applications, Advantages and Disadvantages of Linked List A Linked List is a linear data structure that is used to store a collection of data with the help of nodes. Please remember the following points before moving forward.The consecutive elements are connected by pointers / references.The last node of the linked list points to null.The entry point of a
4 min read
Linked List vs Array Array: Arrays store elements in contiguous memory locations, resulting in easily calculable addresses for the elements stored and this allows faster access to an element at a specific index.Data storage scheme of an arrayLinked List: Linked lists are less rigid in their storage structure and element
2 min read
Types of Linked List
Basic Operations on Linked List
Insertion in Linked ListInsertion in a linked list involves adding a new node at a specified position in the list. There are several types of insertion based on the position where the new node is to be added:At the front of the linked list Before a given node.After a given node.At a specific position.At the end of the link
4 min read
Search an element in a Linked List (Iterative and Recursive)Given a linked list and a key, the task is to check if key is present in the linked list or not. Examples:Input: 14 -> 21 -> 11 -> 30 -> 10, key = 14Output: YesExplanation: 14 is present in the linked list.Input: 6 -> 21 -> 17 -> 30 -> 10 -> 8, key = 13Output: NoExplanatio
12 min read
Find Length of a Linked List (Iterative and Recursive)Given a Singly Linked List, the task is to find the Length of the Linked List.Examples:Input: LinkedList = 1->3->1->2->1Output: 5Explanation: The linked list has 5 nodes.Input: LinkedList = 2->4->1->9->5->3->6Output: 7 Explanation: The linked list has 7 nodes.Input: Lin
11 min read
Reverse a Linked ListGiven a linked list, the task is to reverse the linked list by changing the links between nodes.Examples: Input: head: 1 -> 2 -> 3 -> 4 -> NULLOutput: head: 4 -> 3 -> 2 -> 1 -> NULLExplanation: Reversed Linked List: Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOut
15+ min read
Deletion in Linked ListDeleting a node in a Linked List is an important operation and can be done in three main ways: removing the first node, removing a node in the middle, or removing the last node.In this article, we will explore deletion operation on Linked List for all the above scenarios. Types of Deletion in Linked
3 min read
Delete a Linked List node at a given positionGiven a singly linked list and a position (1-based indexing), the task is to delete a linked list node at the given position.Note: Position will be valid (i.e, 1 <= position <= linked list length)Example: Input: position = 2, Linked List = 8->2->3->1->7Output: Linked List = 8->3
8 min read
Write a function to delete a Linked ListGiven a linked list, the task is to delete the linked list completely.Examples:Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULLOutput: NULLExplanation: Linked List is Deleted.Input: head: 1 -> 12 -> 1 -> 4 -> 1 -> NULLOutput: NULLExplanation: Linked List is Deleted.Table of C
9 min read
Write a function to get Nth node in a Linked ListGiven a LinkedList and an index (1-based). The task is to find the data value stored in the node at that kth position. If no such node exists whose index is k then return -1.Example:Â Input: 1->10->30->14, index = 2Output: 10Explanation: The node value at index 2 is 10 Input: 1->32->12
11 min read
Program for Nth node from the end of a Linked ListGiven a Linked List of M nodes and a number N, find the value at the Nth node from the end of the Linked List. If there is no Nth node from the end, print -1.Examples:Input: 1 -> 2 -> 3 -> 4, N = 3Output: 2Explanation: Node 2 is the third node from the end of the linked list.Input: 35 ->
14 min read
Top 50 Problems on Linked List Data Structure asked in SDE Interviews A Linked List is a linear data structure that looks like a chain of nodes, where each node is a different element. Unlike Arrays, Linked List elements are not stored at a contiguous location. Here is the collection of the Top 50 list of frequently asked interview questions on Linked Lists. Problems
3 min read