Back to All Algorithms
Doubly Linked List
Category: Data Structures | Time: O(n) Search | Space: O(n)
Visualization
Visual representation of the data structure
Enter commands to build the doubly linked list.
About Doubly Linked List

A Doubly Linked List is a type of linked list where each node contains a data field and two pointers: one to the next node in the sequence (next pointer) and one to the previous node (prev pointer). This bidirectional linking allows for traversal in both directions.

Code Editor
The code is for reference. Editing it won't affect the visualization.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Variable State
{
  "status": "Initializing Doubly Linked List"
}
Doubly Linked List: How It Works
Last updated October 28, 2026

Overview

A doubly linked list gives each node two pointers: next to the following node and prev to the previous one. The list usually tracks both the head and the tail. The extra pointer costs memory but allows walking in both directions and removing a node in O(1) when you already hold a reference to it.

That O(1) removal is what makes it the standard companion of a hash map in an LRU cache: the map finds the node instantly, and the doubly linked list moves it to the front or evicts the tail without searching.

The default commands insert 10, 20 and 30, then delete 20. Deleting a middle node updates two pointers: 10.next now points to 30 and 30.prev points back to 10. The list ends as 10 <-> 30 with head 10 and tail 30.

Step by step

  1. Insert at the end: set newNode.prev = tail, tail.next = newNode, then tail = newNode.
  2. Delete a node: set node.prev.next = node.next and node.next.prev = node.prev.
  3. If the deleted node was the head or tail, move that pointer as well.
  4. Traverse forwards from the head with next, or backwards from the tail with prev.

Complexity

CaseCostWhy
Access / searchO(n)Still sequential, although you can start from either end.
Insert / delete at either endO(1)Head and tail pointers are kept.
Delete a known nodeO(1)Both neighbours are reachable directly.
SpaceO(n)Two pointers per node.

When to use it

  • LRU and LFU caches (paired with a hash map).
  • Browser history or editors that need both back and forward navigation.
  • Implementing a deque.

Common mistakes

  • Updating next but forgetting prev (or the reverse) corrupts backward traversal in ways that are hard to spot.
  • Edge cases: deleting the only node must set both head and tail to null.
  • Sentinel head and tail nodes remove most of these special cases and are worth using.
Worked Example: Doubly Linked List Dry Run
Generated from the same trace that drives the visualizer above, using the default input.

A dry run is the best way to check your understanding of Doubly Linked List: follow each row, predict the next one, and compare. The run below takes 11 steps in total. The Line column refers to the line numbers in the code listing above.

Input
insert 10,insert 20,insert 30,delete 20
Result
status = Operations complete
StepLineWhat happens
11status = Initializing Doubly Linked List
213action = insert(10)
322status = Inserted 10, head = 10, tail = 10
413action = insert(20)
522status = Inserted 20, head = 10, tail = 20
613action = insert(30)
722status = Inserted 30, head = 10, tail = 30
813action = delete(20)
913action = delete(20), status = Found 20 at index 1
1013status = Deleted 20, head = 10, tail = 30
1123status = Operations complete
Frequently Asked Questions
About Doubly Linked List

What is a Doubly Linked List?

A Doubly Linked List is a more complex type of linked list where each node has a pointer to the next node as well as a pointer to the previous node. This allows for traversal in both forward and backward directions.

What are the advantages of a Doubly Linked List over a Singly Linked List?

The main advantage is bidirectional traversal. You can move forwards and backwards. This makes some operations, like deleting a node (if you have a pointer to it) or inserting a node before another, more efficient (O(1)) because you don't have to traverse the list to find the previous node.

What are the disadvantages?

The main disadvantage is that it requires extra space for the `prev` pointer in each node. Additionally, the insertion and deletion operations are slightly more complex because you have to manage two pointers (`next` and `prev`) instead of just one.

What does a node in a Doubly Linked List contain?

Each node contains three parts: the data, a pointer to the next node (`next`), and a pointer to the previous node (`prev`).

How does deletion work in a Doubly Linked List?

To delete a node, you update the `next` pointer of the previous node to point to the current node's `next`, and you update the `prev` pointer of the next node to point to the current node's `prev`. This effectively 'unlinks' the node from the chain.

What are the time complexities for operations?

  • Access/Search: O(n).
  • Insertion/Deletion at beginning/end: O(1) (if a tail pointer is maintained).
  • Insertion/Deletion at a given node: O(1).

Where are Doubly Linked Lists used?

They are used in applications that require frequent forward and backward traversal, such as in the 'undo/redo' functionality of a text editor, or in representing the history of visited pages in a web browser.

Where can I learn more?

You can find a more detailed explanation on GeeksforGeeks.
Related Interview Questions
Practice with real problems