algoritmauzmanı
TREN
3.2Doubly Linked List
Lesson 3.2.1

What is a doubly linked list?

In a singly linked list every knows only the next one; there is no way back. In a every node also knows the previous one. In this lesson you will see how this small addition changes the node, the list and the cost of the operations.

What you will learn
  • The three fields of a node in a doubly linked list and how it is defined in C, Java and Python
  • What , and the NULL values at both ends of the list mean
  • What becomes easier compared with a singly linked list, and what it costs in memory
5 min

1The three fields of a node

A in a doubly linked list carries three fields. In the middle is data, which holds the value. On the right is the field, which holds the address of the node after it; this is the same as in a singly linked list. On the left is the field, which holds the address of the node before it.

prevdatanextNULLNULL7?0x1a90
A single node: prev on the left, data in the middle, next on the right. There are no other nodes in the list, so both links are NULL.

In C the node is defined with a . In all the code on this site the field is written before :

struct Node {    int data;    struct Node* prev;   // address of the previous node    struct Node* next;   // address of the next node};

In Java the two fields have the same type, so they can share a line. Both of a new object start as null:

class Node {    int data;    Node prev, next;    Node(int data) { this.data = data; }}

In Python the two fields are set to None in the constructor:

class Node:    def __init__(self, data):        self.data = data        self.prev = None        self.next = None

2head, tail and the NULLs at both ends

You still enter the list through , which points to the first node. Doubly linked lists usually also keep a pointer to the last node. That way both ends of the list can be reached without walking it.

NULLNULL12?0x1a407?0x1a903?0x1b20headtail
A doubly linked list with three nodes. Black arrows are next links and blue arrows are prev links. The first node's prev field and the last node's next field are NULL.

Between two neighbouring nodes there are two links: the left node's field points to the right node, and the right node's field points to the left one. At both ends of the list one of these links is missing. The first node's field is because nothing is in front of it. The last node's field is NULL because nothing comes after it. When the list is empty, and are NULL.

In a correctly built doubly linked list these two rules hold for every node: if d->next is not NULL then d->next->prev == d, and if d->prev is not NULL then d->prev->next == d. Check these two rules when you test insertion and deletion code.

3Compared with a singly linked list

Singly linkedDoubly linked
Pointers per node1 (next)2 (prev, next)
Walking backwardNot possibleYes, with prev
Inserting at the headO(1)O(1)
Deleting a node you holdO(n): the previous node is searched forO(1): the previous node is in prev
Links changed by insert and delete1 or 22 to 4

The biggest gain is in deletion. To delete a node from a singly linked list you have to change the field of the previous node, and the only way to find that node is to the list from the start. In a doubly linked list the previous node is right there in the field of the node being deleted. If you hold the node, deletion takes .

The price is memory. Every node carries one more ; on a 64-bit system that is 8 bytes per node. In a list that stores a single int per node, the pointers take more room than the values. The second price is care: every insertion and deletion must update the links in both directions together. Forgetting one causes bugs that stay hidden when you walk forward and show up when you walk backward.

4Where is it used?

  • Wherever you need to move both ways: the back and forward buttons of a browser, the undo and redo history of a text editor.
  • Wherever an element must be taken out of the middle quickly and moved to the front. A cache that evicts the least recently used item (LRU) uses a hash table together with a doubly linked list.
  • In standard libraries: Java's LinkedList class is a doubly linked list.

5In this section

In the next lessons you will see the basic operations of a doubly linked list one at a time, with step-by-step drawings: inserting at the head, inserting at the end, inserting at a position, deleting a node and traversing in both directions. Each lesson links to its singly linked counterpart so you can compare them side by side.

6Try it

In a correctly built doubly linked list with three nodes, how many pointer fields are NULL?

Show the answer

The answer is B. The first node's field and the last node's field are NULL. Every other field points to a neighbour. In a one-node list both of these fields are in the same node.

Why does deleting a node you hold take O(1) in a doubly linked list but O(n) in a singly linked list?

Show the answer

The answer is B. The field of the node in front of the deleted one must change. In a singly linked list that node is found by walking from the start. In a doubly linked list d->prev gives it directly. The details are in Deleting a node.

Lessons you mark show up in the curriculum. This is kept on this device only.