3.2Doubly Linked List
What is a doubly linked list?
In a singly linked list every node knows only the next one; there is no way back. In a doubly linked list 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.
1The three fields of a node
A node in a doubly linked list carries three fields. In the middle is data, which holds the value. On the right is the next 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 prev field, which holds the address of the node before it.
In C the node is defined with a struct. In all the code on this site the prev field is written before next:
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 references 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 = None2head, tail and the NULLs at both ends
You still enter the list through head, which points to the first node. Doubly linked lists usually also keep a tail pointer to the last node. That way both ends of the list can be reached without walking it.
Between two neighbouring nodes there are two links: the left node's next field points to the right node, and the right node's prev field points to the left one. At both ends of the list one of these links is missing. The first node's prev field is NULL because nothing is in front of it. The last node's next field is NULL because nothing comes after it. When the list is empty, head and tail 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 linked | Doubly linked | |
|---|---|---|
| Pointers per node | 1 (next) | 2 (prev, next) |
| Walking backward | Not possible | Yes, with prev |
| Inserting at the head | O(1) | O(1) |
| Deleting a node you hold | O(n): the previous node is searched for | O(1): the previous node is in prev |
| Links changed by insert and delete | 1 or 2 | 2 to 4 |
The biggest gain is in deletion. To delete a node from a singly linked list you have to change the next field of the previous node, and the only way to find that node is to traverse the list from the start. In a doubly linked list the previous node is right there in the prev field of the node being deleted. If you hold the node, deletion takes O(1).
The price is memory. Every node carries one more pointer; 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
LinkedListclass 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?
Why does deleting a node you hold take O(1) in a doubly linked list but O(n) in a singly linked list?
The answer is B. The next 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.
Show the answer
The answer is B. The next 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.