Open In App

Types of Linked List

Last Updated : 17 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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:


Singly-Linked-List


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


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

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.

Doubly-Linked-List


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


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

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:


Circular-Linked-List


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


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

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.

Doubly-Circular-Linked-List


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


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

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.



Next Article

Similar Reads

three90RightbarBannerImg