A doubly linked list is a linear data structure whose nodes each hold data, a next link to the following node and a prev link to the previous one, so it can be walked in both directions. A node you already hold is removed in O(1), with no walk to find its predecessor. This page explains how it works, with step-by-step animations of insertion in the middle, deletion in the middle and deletion at the end with a tail from the VisiGrab app.
// how doubly linked lists work
A doubly linked list is a linear data structure that stores a sequence of elements in nodes. Each node holds its data and two links: next to the following node and prev to the previous one. The list is reached through its head, a link to the first node, and may also keep a tail, a link to the last. Unlike a singly linked list, it can be walked in both directions. It is also called a two-way linked list.
A node has three fields: prev, data and next. In the app the data is a letter, and the node holding A is nodeA. The first node's prev and the last node's next are null (None in Python, nullptr in C++): they mark the ends of the list.
Note
In C and C++ a link is a pointer; in Java and Python it is a reference.
To traverse forward, start at head and follow the next links until one is null. To traverse backward, start at the last node and follow the prev links. A tail gives that start in O(1); with head only, an O(n) forward walk must reach it first. The app shows each direction by dimming the other chain.
At the beginning, insertion sets node.next = head and head.prev = node, then head = node: O(1), one step more than in a singly linked list. An empty list needs only head = node.
Insertion in the middle puts a node after a given one. A variable pred, short for predecessor, walks from head to that node. Four links are then set, in this order: node.next = pred.next, node.prev = pred, pred.next.prev = node, pred.next = node. The third step reaches the old successor through pred.next, so pred.next changes last. In the player, C goes after B in A ⇄ B ⇄ D.
The walk costs O(n) and the link changes O(1), so insertion in the middle is O(n). Inserting after the last node skips the third step, as pred.next is null. At the end, pred walks to the last node, then node.prev = pred and pred.next = node: O(n). A tail makes it O(1). The app shows insertion at the beginning and at the end step by step.
A node is deleted by making its neighbors link past it. One variable is enough: cur, short for current, walks from head to the node, whose neighbors are cur.prev and cur.next. Then cur.prev.next = cur.next and cur.next.prev = cur.prev. A singly linked list needs a second variable, pred, one node behind cur. In the player, C is deleted from A ⇄ B ⇄ C ⇄ D.
Finding C costs O(n), unlinking it O(1). C's own links still lead to B and D, which does no harm. If cur is the last node, its next is null and the second step is skipped. If cur is the first node, its prev is null, so deletion at the beginning runs instead: head = head.next, then head.prev = null unless the list is now empty, in O(1). At the end with head only, cur walks to the last node, steps back with cur = cur.prev and sets cur.next = null: O(n), with one variable. The app shows deletion at the beginning and at the end step by step.
Note
In C++ a removed node must also be freed with delete, or its memory leaks. Java and Python reclaim it once nothing refers to it.
Once you hold a node, from a hash map or an iterator, removing it takes O(1): its prev link leads to its predecessor, so both neighbors are relinked at once. A singly linked list finds the predecessor only by a walk from head, in O(n). Inserting before a node you hold is O(1) for the same reason. By value or by position, removal stays O(n): the node must be found first.
A double-ended doubly linked list also keeps tail, a link to the last node. Insertion at the end then needs no walk: node.prev = tail, tail.next = node, tail = node, in O(1). Deletion at the end is O(1) too, because the new last node is tail.prev. A singly linked list with a tail finds that node only by a walk from head, so there it stays O(n). In the player, E is deleted from A ⇄ B ⇄ C ⇄ D ⇄ E with two assignments: tail = tail.prev, then tail.next = null.
Inserting into an empty list or deleting the only node changes both head and tail. Sentinel (dummy) nodes at both ends avoid such special cases.
A singly linked list node has one link, next; a doubly linked list node also has prev, to the node before it. The extra link lets the list be walked backward and lets a node you hold be deleted, or a node be inserted before it, in O(1) instead of O(n), at the cost of one more link per node and more links to update: four instead of two for an insert in the middle.
| Operation | Head only | Head and tail |
|---|---|---|
| Access by index | O(n); the first node O(1) | O(n); the first and last nodes O(1) |
| Search | O(n) | O(n) |
| Insert or delete at the beginning | O(1) | O(1) |
| Insert or delete in the middle, by value or position | O(n) | O(n) |
| Insert next to, or delete, a node you hold | O(1) | O(1) |
| Insert at the end | O(n) | O(1) |
| Delete at the end | O(n) | O(1) |
Space is O(n), with two links per node, one more than in a singly linked list.
| Property | Singly linked list | Doubly linked list |
|---|---|---|
| Links per node | One: next | Two: next and prev |
| Traversal | Forward only | Forward and backward |
| Delete or insert before a node you hold | O(n) | O(1) |
| Delete at the end, with a tail | O(n) | O(1) |
| Links changed by an insert in the middle | 2 | 4 |
| Links changed by a delete in the middle | 1 | 2 |
| Variables that walk to delete in the middle | Two: pred and cur | One: cur |
Against an array, a doubly linked list wins at the front and at a node already held: nothing shifts. The array keeps O(1) access by index, cache-friendly memory and less memory per element.
In a circular doubly linked list the last node's next links to the first node and the first node's prev to the last, so no link is null. The last node is head.prev, reached in O(1) without a tail. A traversal stops when it returns to its start node.
Java's java.util.LinkedList is a doubly linked list; access by index walks from the closer end, still O(n). C++'s std::list is usually a doubly linked list, with O(1) insert and erase at an iterator; std::forward_list is singly linked. Python has no built-in linked list; CPython's collections.deque is a doubly linked list of blocks of items.
The full source code is on GitHub, in Python, Java and C++.
O(1).O(1).Advantages: two-way traversal, O(1) removal of a node you hold, O(1) work at both ends with a tail, and no shifting. Disadvantages: an extra link per node, no direct access by index and poor cache locality. Every change must also keep both chains consistent: a missed prev update breaks the backward walk, and the forward chain too once a delete follows that prev link.
// in the app
In VisiGrab the doubly linked list is a 60-slide show, from what a node is to operations with a tail, played one step at a time.
The node's pseudocode, then null at both ends of the list, written as null, None or nullptr to match your code language. Forward and backward traversal, with one chain of links dimmed at a time.
Insertion at the beginning and at the end, and deletion at the end and at the beginning, each a chapter of its own. A bonus chapter then gives the list a tail and appends E with no walk before deleting it again.
Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.
Two tabs: Info, with the definition, advantages and limitations, and Efficiency, with time and space complexity and a table for a list with a head only and with a head and tail.
A doubly linked list with six insert and delete methods, in the order the slide show plays them, then a double-ended doubly linked list's insertLast and deleteLast. The C++ version frees every removed node.
Ten questions to check what you have learned before moving on.
// build your own
Download VisiGrab and step through both directions, six insert and delete operations and the tail yourself.
The doubly linked list is one of 30+ topics. See them all