Introduction to Doubly Linked List – Data Structure and Algorithm Tutorials
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.
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:
- Doubly Linked List meaning in DSA
- Applications, Advantages and Disadvantages of Doubly Linked List
- Memory efficient doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List
Problems on Doubly Linked List:
- Easy Problems
- Program to find size of Doubly Linked List
- Find the largest node in Doubly linked list
- Find pairs with given sum in doubly linked list
- Large number arithmetic using doubly linked list
- Check if a doubly linked list of characters is palindrome or not
- Insert value in sorted way in a sorted doubly linked list
- Rotate Doubly linked list by N nodes
- Intermediate Problems
- Insertion Sort for Doubly Linked List
- QuickSort on Doubly Linked List
- Merge Sort for Doubly Linked List
- Remove duplicates from a sorted doubly linked list
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x
- Sort the biotonic doubly linked list
- Create a Doubly Linked List from a Ternary Tree
- Delete all occurrences of a given key in a doubly linked list
- Sorted merge of two sorted doubly circular linked lists
- Extract Leaves of a Binary Tree in a Doubly Linked List
- Hard Problems
- Remove duplicates from an unsorted doubly linked list
- Sort a k sorted doubly linked list
- Reverse a doubly linked list in groups of given size
- Priority Queue using doubly linked list
- Convert a given Binary Tree to Doubly Linked List
- Convert a given Binary Tree to Doubly Linked List | Set
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- The Great Tree-List Recursion Problem.
- ‘Practice Problems’ on Linked List
- ‘Videos’ on Linked List
- ‘Quizzes’ on Linked List
- Recent articles on Doubly Linked List