Introduction to Doubly Linked List – Data Structure and Algorithm Tutorials

Last Updated : 18 Mar, 2024

What is Doubly Linked List?

A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the previous node as well as the next node of the linked list.

Doubly Linked List

Doubly Linked List

Given below is a representation of a DLL node:

C++

// Node of a doubly linked list
class Node {
public:
    int data;
  
    // Pointer to next node in DLL
    Node* next;
  
    // Pointer to previous node in DLL
    Node* prev;
};

C

// Node of a doubly linked list

struct Node {
    int data;
  
    // Pointer to next node in DLL
    struct Node* next;
  
    // Pointer to previous node in DLL
    struct Node* prev;
};

Java

// Class for Doubly Linked List

public class DLL {
    
    // Head of list
    Node head;

    // Doubly Linked list Node
    class Node {
        int data;
        Node prev;
        Node next;

        // Constructor to create a new node
        // next and prev is by default initialized as null
        Node(int d) { data = d; }
    }
}

Python3

# Node of a doubly linked list

class Node:
    def __init__(self, next=None, prev=None, data=None):
        
        # reference to next node in DLL
        self.next = next
        
        # reference to previous node in DLL
        self.prev = prev
        self.data = data

C#

// Class for Doubly Linked List

public class DLL {
    
    // head of list
    Node head;

    // Doubly Linked list Node
    public class Node {
        public int data;
        public Node prev;
        public Node next;

        // Constructor to create a new node
        // next and prev is by default initialized as null
        Node(int d) { data = d; }
    }
}

Javascript

<script>
// Class for Doubly Linked List
    var head; // head of list

    /* Doubly Linked list Node */
     class Node {
        // Constructor to create a new node
            // next and prev is by default initialized as null
            constructor(val) {
                this.data = val;
                this.prev = null;
                this.next = null;
            }
        }
        
</script>

Advantages of Doubly Linked List over the singly linked list:

  • A DLL can be traversed in both forward and backward directions. 
  • The delete operation in DLL is more efficient if a pointer to the node to be deleted is given. 
  • We can quickly insert a new node before a given node. 
  • In a singly linked list, to delete a node, a pointer to the previous node is needed. To get this previous node, sometimes the list is traversed. In DLL, we can get the previous node using the previous pointer. 

Disadvantages of Doubly Linked List over the singly linked list:

  • Every node of DLL Requires extra space for a previous pointer. It is possible to implement DLL with a single pointer though (See this and this). 
  • All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with the next pointers. For example in the following functions for insertions at different positions, we need 1 or 2 extra steps to set the previous pointer.

Applications of Doubly Linked List:

  • It is used by web browsers for backward and forward navigation of web pages 
  • LRU ( Least Recently Used ) / MRU ( Most Recently Used ) Cache are constructed using Doubly Linked Lists. 
  • Used by various applications to maintain undo and redo functionalities. 
  • In Operating Systems, a doubly linked list is maintained by thread scheduler to keep track of processes that are being executed at that time.

Topics:

Introduction:

  1. Doubly Linked List meaning in DSA
  2. Applications, Advantages and Disadvantages of Doubly Linked List
  3. Memory efficient doubly linked list
  4. XOR Linked List – A Memory Efficient Doubly Linked List

Basic Operations:

  1. Doubly Linked List Insertion
  2. Reverse a Doubly Linked List
  3. Delete a node in a Doubly Linked List

Problems on Doubly Linked List:

Quick Links :



Share your thoughts in the comments