Doubly Linked List, step by step.

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 a doubly linked list works

What is a doubly linked list?

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.

Nodes: prev, data and next

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.

Five nodes in a row, A, B, C, D and E, each drawn as a box with three cells: prev on the left, the letter in the middle and next on the right. Between neighboring nodes an upper arrow runs right from next to the following node, and a lower arrow runs left from prev to the previous node. A head label links to A, A's prev and E's next are marked null, and one node's three cells are labeled prev, data and next.
Each node holds its data and two links: head refers to A, A's prev is null, and so is E's next.

Traversing forward and backward

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.

The same five-node list, A to E, drawn twice. In the first copy the next arrows, pointing right, are bright and the prev arrows are dimmed. In the second copy the prev arrows, pointing left, are bright and the next arrows are dimmed.
Forward traversal follows the next links from A to E; backward traversal follows the prev links from E to A.

Inserting into a doubly linked list

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.

Insert in the middle

All steps as text
  1. The list is A ⇄ B ⇄ D, and head refers to A. Step forward to insert C after B.
  2. Now we insert a new node in the middle — right after nodeB ("in the middle" just means somewhere between existing nodes, not at the front or the end).
  3. But first we have to reach the node we want to insert after. A variable called pred (short for predecessor) walks the list to find it; it starts at the head, on nodeA.
  4. We follow the next links until pred lands on the node we want to insert after — pred = nodeB. Now we hold the predecessor and can splice the new node in right after it.
  5. Next, we create the new node that holds C. Its next and prev are still unset, ready to be linked in right after pred.
  6. First, link nodeC forward: point its next at nodeD — the node pred.next already points to — so nodeC.next = pred.next.
  7. And backward: point nodeC's prev at nodeB, the predecessor itself — nodeC.prev = pred. Now nodeC references both of its neighbors.
  8. Now re-point the neighbors. nodeD — that's pred.next — used to point back to nodeB; swing its prev to the new node: pred.next.prev = nodeC.
  9. And pred's own next, which pointed forward to nodeD, now points to the new node: pred.next = nodeC. The new node is now fully linked into the chain.
  10. Insertion in the middle is complete.
  11. The list reads A ⇄ B ⇄ C ⇄ D. Reaching B is a walk from the head, O(n) in general; changing the four links takes O(1).
Doubly Linked List — Insert in the middle. Last step: The list reads A ⇄ B ⇄ C ⇄ D. Reaching B is a walk from the head, O(n) in general; changing the four links takes O(1).

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.

Deleting from a doubly linked list

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.

Delete from the middle

All steps as text
  1. The list is A ⇄ B ⇄ C ⇄ D, and head refers to A. Step forward to delete C.
  2. Now we delete a node from the middle — we'll remove nodeC.
  3. We use a variable called cur (short for current), shown as a marker; it starts at the head, on nodeA, and will walk along the list until it lands on the node we want to delete.
  4. The node is found. From here, cur references the node we'll delete.
  5. First, the node before cur bridges over it to the node after: cur.prev.next = cur.next.
  6. Then the node after cur bridges back over it: cur.next.prev = cur.prev. Now no link leads to nodeC.
  7. With no links left to it, nodeC is no longer part of the list. Deletion in the middle is complete.
  8. The list reads A ⇄ B ⇄ D. Reaching C is a walk from the head, O(n) in general; changing the two links takes O(1). One variable was enough: C's prev link led to B.
Doubly Linked List — Delete from the middle. Last step: The list reads A ⇄ B ⇄ D. Reaching C is a walk from the head, O(n) in general; changing the two links takes O(1). One variable was enough: C's prev link led to B.

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.

Why deleting a node you hold is O(1) in a doubly linked list

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.

Doubly linked list with a tail

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.

Delete at the end with a tail

All steps as text
  1. The list is A ⇄ B ⇄ C ⇄ D ⇄ E; head refers to A and tail to E. Step forward to delete E.
  2. Now delete the last node, nodeE. The tail points right at it — nothing to search for.
  3. Step the tail back to the node before it. Because the list is doubly linked, that's just nodeE's prev — tail = tail.prev — landing on nodeD.
  4. Cut nodeD's forward link — tail.next = null — and nodeD becomes the new last node.
  5. With nothing pointing to it, nodeE is removed.
  6. The list reads A ⇄ B ⇄ C ⇄ D, and tail refers to D. No walk was needed, so deletion at the end with a tail takes O(1). A singly linked list needs an O(n) walk here, even with a tail.
Doubly Linked List — Delete at the end with a tail. Last step: The list reads A ⇄ B ⇄ C ⇄ D, and tail refers to D. No walk was needed, so deletion at the end with a tail takes O(1). A singly linked list needs an O(n) walk here, even with a tail.

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.

Doubly vs singly linked list: what is the difference?

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.

Doubly linked list time complexity

OperationHead onlyHead and tail
Access by indexO(n); the first node O(1)O(n); the first and last nodes O(1)
SearchO(n)O(n)
Insert or delete at the beginningO(1)O(1)
Insert or delete in the middle, by value or positionO(n)O(n)
Insert next to, or delete, a node you holdO(1)O(1)
Insert at the endO(n)O(1)
Delete at the endO(n)O(1)

Space is O(n), with two links per node, one more than in a singly linked list.

Doubly vs singly linked list

PropertySingly linked listDoubly linked list
Links per nodeOne: nextTwo: next and prev
TraversalForward onlyForward and backward
Delete or insert before a node you holdO(n)O(1)
Delete at the end, with a tailO(n)O(1)
Links changed by an insert in the middle24
Links changed by a delete in the middle12
Variables that walk to delete in the middleTwo: pred and curOne: 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.

Circular doubly linked list

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.

Doubly linked lists in Python, Java and C++

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++.

Where doubly linked lists are used: advantages and disadvantages

  • Back and forward history: back follows prev, forward follows next.
  • Undo and redo, moving through a list of edits both ways.
  • An LRU cache: a hash map finds a key's node, and a doubly linked list moves it to the front or drops the least recently used node at the end, in O(1).
  • A deque (double-ended queue): with a tail, adding and removing at both ends take 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.

Three animations here.
The whole topic 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.

Nodes, null ends, both ways

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.

Four more operations and a tail

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.

Forward and back

Step through every slide and step back, or jump to any slide from the list. The show reopens where you left it.

Theory and complexity

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.

Python, Java and C++

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.

Quiz

Ten questions to check what you have learned before moving on.

Build a doubly linked list
link by link.

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