Doubly Linked List Tutorial
Last Updated :
23 Jul, 2025
A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both directions.
Doubly Linked ListRepresentation of Doubly Linked List in Data Structure
In a data structure, a doubly linked list is represented using nodes that have three fields:
- Data
- A pointer to the next node (next)
- A pointer to the previous node (prev)
Node Structure of Doubly Linked ListNode Definition
Here is how a node in a Doubly Linked List is typically represented:
C++
struct Node {
// To store the Value or data.
int data;
// Pointer to point the Previous Element
Node* prev;
// Pointer to point the Next Element
Node* next;
// Constructor
Node(int d) {
data = d;
prev = next = nullptr;
}
};
C
struct Node {
// To store the Value or data.
int data;
// Pointer to point the Previous Element
Node* prev;
// Pointer to point the Next Element
Node* next;
};
// Function to create a new node
struct Node *createNode(int new_data) {
struct Node *new_node = (struct Node *)
malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
new_node->prev = NULL;
return new_node;
}
Java
class Node {
// To store the Value or data.
int data;
// Reference to the Previous Node
Node prev;
// Reference to the next Node
Node next;
// Constructor
Node(int d) {
data = d;
prev = next = null;
}
};
Python
class Node:
def __init__(self, data):
# To store the value or data.
self.data = data
# Reference to the previous node
self.prev = None
# Reference to the next node
self.next = None
C#
class Node
{
// To store the value or data
public int Data;
// Pointer to the next node
public Node Next;
// Pointer to the previous node
public Node Prev;
// Constructor
public Node(int d)
{
Data = d;
Prev = Next = null;
}
}
JavaScript
class Node {
constructor(data)
{
// To store the value or data.
this.data = data;
// Reference to the previous node
this.prev = null;
// Reference to the next node
this.next = null;
}
}
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in the list, and a pointer to the previous node in the list. By linking these nodes together through the next and prev pointers, we can traverse the list in both directions (forward and backward), which is a key feature of a Doubly Linked List.
1. Traversal in Doubly Linked List
Traversal in a Doubly Linked List involves visiting each node, processing its data, and moving to the next or previous node using the forward (next
) and backward (prev
) pointers.
Step-by-Step Approach for Traversal:
- Start from the head of the list.
- Traverse forward:
- Visit the current node and process its data (e.g., print it).
- Move to the next node using
current = current->next
. - Repeat the process until the end of the list (
current == NULL
).
- Optionally, traverse backward:
- Start from the tail (last node).
- Visit the current node and process its data.
- Move to the previous node using
current = current->prev
. - Repeat the process until the beginning of the list (
current == NULL
).
Traversal is useful for displaying or processing all nodes in a doubly linked list.
To read more about Traversal Operation Refer, Traversal in Doubly Linked List
2. Finding Length of Doubly Linked List
A Doubly Linked List (DLL) is a type of linked list where each node has two pointers:
- One pointing to the next node in the sequence.
- One pointing to the previous node in the sequence.
To find the length of a doubly linked list, we need to traverse the list while counting the nodes.
Step-by-Step Approach for finding length:
- Initialize a counter: Start with a counter variable (
count = 0
). - Set a pointer to the head node: Use a pointer (
current
) and initialize it to the head of the linked list. - Traverse the list:
- While the pointer (
current
) is not NULL
, increment the count
by 1
. - Move to the next node (
current = current.next
).
- Stop at the end of the list: When the pointer reaches
NULL
, stop the loop. - Return the count: The final value of
count
gives the length of the doubly linked list.
To read more about Finding Length of DLL Refer, Program to find size of Doubly Linked List
3. Insertion in a Doubly Linked List
Insertion in a Doubly Linked List (DLL) involves adding a new node at a specific position while maintaining the connections between nodes. Since each node contains a pointer to both the previous and next node, insertion requires adjusting these pointers carefully.
There are three primary types of insertion in a DLL:
1. Insertion at the Beginning
- Create a new node with the given data.
- Set the
next
pointer of the new node to the current head. - If the list is not empty, update the
prev
pointer of the current head to point to the new node. - Update the head of the list to the new node.
Read more about Insertion at the Beginning Refer, Insert a Node at Front/Beginning of Doubly Linked List
2. Insertion at the End
- Create a new node with the given data.
- If the list is empty, set the new node as the head.
- Traverse the list until the last node is found.
- Set the
next
pointer of the last node to the new node. - Set the
prev
pointer of the new node to the last node.
Read more about Insertion at the End Refer, Insert a Node at the end of Doubly Linked List
3. Insertion at a Specific Position
- Create a new node with the given data.
- If inserting at the beginning, follow the steps for insertion at the start.
- Traverse the list to find the node after which insertion is needed.
- Set the
next
pointer of the new node to the next node of the current position. - Set the
prev
pointer of the new node to the current node. - Update the
prev
pointer of the next node to point to the new node (if it exists). - Update the
next
pointer of the previous node to point to the new node.
Read more about Insertion at a specific position Refer, Insert a Node at a specific position in Doubly Linked List
4. Deletion in a Doubly Linked List
Deletion in a Doubly Linked List (DLL) involves removing a node while maintaining the integrity of the list. Since each node contains pointers to both its previous and next nodes, deletion requires careful pointer adjustments to ensure no broken links occur.
Types of Deletion in a Doubly Linked List
1. Deletion at the Beginning
- Check if the list is empty; if it is, return as there is nothing to delete.
- Store the current head node in a temporary variable.
- Move the head pointer to the next node.
- If the new head exists, update its
prev
pointer to NULL
. - Delete the old head node to free memory.
Read more about Deletion at the Beginning Refer, Deletion at beginning (Removal of first node) in a Doubly Linked List
2. Deletion at the End
- Check if the list is empty; if it is, return.
- Traverse the list to find the last node.
- Store the last node in a temporary variable.
- Update the
next
pointer of the second-last node to NULL
, making it the new tail. - Delete the last node to free memory.
Read more about Deletion at the End Refer, Deletion at end (Removal of last node) in a Doubly Linked List
3. Deletion at a Specific Position
- Check if the list is empty; if it is, return.
- Traverse the list to find the node to be deleted.
- Store the node to be deleted in a temporary variable.
- Update the
next
pointer of the previous node to point to the next node. - Update the
prev
pointer of the next node to point to the previous node (if it exists). - Delete the target node to free memory.
Read more about Deletion at a specific position Refer, Delete a Doubly Linked List node at a given position
Advantages of Doubly Linked List
- Efficient traversal in both directions: Doubly linked lists allow for efficient traversal of the list in both directions, making it suitable for applications where frequent insertions and deletions are required.
- Easy insertion and deletion of nodes: The presence of pointers to both the previous and next nodes makes it easy to insert or delete nodes from the list, without having to traverse the entire list.
- Can be used to implement a stack or queue: Doubly linked lists can be used to implement both stacks and queues, which are common data structures used in programming.
Disadvantages of Doubly Linked List
- More complex than singly linked lists: Doubly linked lists are more complex than singly linked lists, as they require additional pointers for each node.
- More memory overhead: Doubly linked lists require more memory overhead than singly linked lists, as each node stores two pointers instead of one.
Applications of Doubly Linked List
- Implementation of undo and redo functionality in text editors.
- Cache implementation where quick insertion and deletion of elements are required.
- Browser history management to navigate back and forth between visited pages.
- Music player applications to manage playlists and navigate through songs efficiently.
- Implementing data structures like Deque (double-ended queue) for efficient insertion and deletion at both ends.
Practice Questions on Doubly Linked List
MCQs on Linked List
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA 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:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem