0% found this document useful (0 votes)
13 views

6-2-Types of Linked List

The document outlines various types of linked lists, including singly linked lists, doubly linked lists, circular linked lists, doubly circular linked lists, multilevel linked lists, and skip lists. Each type is described with its structure, creation, traversal methods, and time complexity. Additionally, it mentions header linked lists as a technique rather than a distinct type.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

6-2-Types of Linked List

The document outlines various types of linked lists, including singly linked lists, doubly linked lists, circular linked lists, doubly circular linked lists, multilevel linked lists, and skip lists. Each type is described with its structure, creation, traversal methods, and time complexity. Additionally, it mentions header linked lists as a technique rather than a distinct type.

Uploaded by

shubhrajkumar707
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Types of Linked List

Last Updated : 17 Sep, 2024

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++ C Java Python C# JavaScript

// 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;
}
};

Below is the creation and traversal of Singly Linked List:

C++ C Java Python C# JavaScript


1
// C++ program to illustrate creation

2
// and traversal of Singly Linked List

4
#include <bits/stdc++.h>

5
using namespace std;

7
class Node {

8
public:

9
int data;

10
Node* next;

11
Node(int data) {

12
this->data = data;

13
this->next = nullptr;

14
}

15
};

16

Output

1 2 3

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++ C Java Python C# JavaScript

1
// Define the Node structure

2
struct Node {

3
int data;

4
Node* next;

5
Node* prev;

Below is the creation and traversal of doubly linked list:

C++ C Java Python C# JavaScript

1
// C++ program to illustrate creation

2
// and traversal of doubly Linked List

3
#include <iostream>

4
using namespace std;

6
struct Node {

7
int data;
8
Node* next;

9
Node* prev;

10

11
Node(int x) {

12
data = x;

13
next = nullptr;

14
prev = nullptr;

15
}

16
};

17

18
void forwardTraversal(Node* head) {

19
Node* curr = head;

20
while (curr != nullptr) {

21
cout << curr->data << " ";

22

Output

Forward 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++ C Java Python C# JavaScript

// Define the Node structure


struct Node {
int data;
Node* next;

Node(int value) {
data = value;
next = nullptr;
}
};

Below is the creation and traversal of circular linked list:

C++ C Java Python C# JavaScript

1
// C++ program to illustrate creation

2
// and traversal of Circular Linked List

3
#include <iostream>

4
using namespace std;

6
struct Node {
7
int data;

8
Node* next;

10
Node(int value) {

11
data = value;

12
next = nullptr;

13
}

14
};

15

16

17
void printList(Node* last){

Output

2 3 4

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++ Java Python C# JavaScript

// Structure of a Node
struct Node {
int data;
struct Node* next;
struct Node* prev;
};

Below is the creation and traversal of doubly circular linked list:

C++ C Java Python C# JavaScript

1
// C++ program to illustrate creation

2
// and traversal of doubly circular Linked List

3
#include <iostream>

4
using namespace std;

6
struct Node {

7
int data;

8
Node* next;

9
Node* prev;

10

11
Node(int x) {

12
data = x;

13
next = nullptr;

14
prev = nullptr;

15

16
}
17
};

18

19
void forwardTraversal(Node* head) {

20
Node* curr = head;

21
do {

22
cout << curr->data << " ";

23
curr = curr->next;

24
} while (curr != head);

Output

Forward 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.

You might also like