We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Advantages and Disadvantages of Doubly Linked List
Last updated:
A doubly linked list gives every node a prev reference in addition to next. This page looks at what that extra reference buys you and what it costs, so you can judge when a doubly linked list is the right tool.
Advantages of Doubly Linked List
Traversal in both directions: you can walk from the head towards the tail with next, or from the tail towards the head with prev. Printing in reverse needs no recursion and no extra stack.
O(1) removal of a node you already hold: the node before it is node.prev, so unlinking takes a constant number of steps. In a singly linked list the predecessor must first be found by a walk from the head.
O(1) insertion before a given node: a singly linked list can only insert after a node cheaply; a doubly linked list can do both.
O(1) work at both ends: with head and tail references, adding or removing at the front or the back is constant time. That is why java.util.LinkedList, which implements Deque, is doubly linked.
No fixed capacity: nodes are created only when needed, so the list never has to be resized or copied.
Disadvantages of Doubly Linked List
More memory per node: every node stores one more reference than in a singly linked list (typically 4 extra bytes with compressed references on a 64-bit JVM, 8 without).
More links to keep correct: inserting between two nodes changes four references, and deleting changes two. Forgetting one leaves the forward and backward views of the list inconsistent.
Slightly slower updates: the extra reference updates make each change a little more expensive, although the time complexity is the same.
Still no random access: reaching position k means following links; starting from the nearer end halves the walk but it remains O(n).
Frequently Asked Questions (FAQ)
Q1. What is the main advantage of a doubly linked list over a singly linked list?
It can be traversed in both directions, and a node can be deleted in O(1) when you have a reference to it, because its previous node is available through prev.
Q2. What is the main disadvantage of a doubly linked list?
Each node needs an extra reference (prev), so it uses more memory, and every insert and delete must update more links, which makes the code more complex and error-prone.
Q3. How much extra memory does a doubly linked list need?
One extra reference per node. On a 64-bit JVM with compressed references, that is usually 4 bytes per node, or 8 bytes without compression. For a list of one million nodes, that is several extra megabytes.
Q4. Write Java code that shows the O(1) delete advantage.
RemoveNode.java
public class RemoveNode {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
// No traversal is needed: prev gives us the node before
static Node remove(Node head, Node node) {
if (node.prev != null) {
node.prev.next = node.next;
} else {
head = node.next; // removing the head
}
if (node.next != null) {
node.next.prev = node.prev;
}
return head;
}
}
Q5. Why is deleting the last node faster in a doubly linked list?
In a singly linked list you must walk from the head to find the second-last node, which takes O(n). In a doubly linked list with a tail reference, the second-last node is tail.prev, so the delete takes O(1):
RemoveLast.java
public class RemoveLast {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static Node removeLast(Node tail) {
if (tail == null) {
return null;
}
Node newTail = tail.prev;
if (newTail != null) {
newTail.next = null;
}
return newTail;
}
}
Q6. Is searching faster in a doubly linked list?
No. Searching for a value is still O(n), because you must check the nodes one by one. If you know the index, you can start from whichever end is closer, which halves the steps on average, as java.util.LinkedList.get(i) does, but it remains O(n).
Q7. When is the extra complexity of a doubly linked list not worth it?
When you only move forward and add or remove at the front, such as in a simple stack or a graph adjacency list. A singly linked list is then smaller and simpler.
Q8. Does a doubly linked list make reversal easier?
Yes. You can print it in reverse simply by starting at the tail and following prev. To actually reverse it, swap prev and next in every node, then swap head and tail.
Q9. Write a Java method to print a doubly linked list in reverse order.
PrintReverse.java
public class PrintReverse {
static class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
}
}
static void printReverse(Node tail) {
for (Node current = tail; current != null; current = current.prev) {
System.out.print(current.data + " ");
}
System.out.println();
}
}
No recursion or extra stack is needed, so it uses O(1) extra space.
Q10. Why is java.util.LinkedList a doubly linked list?
Because it implements Deque, which needs O(1) insertion and removal at both ends. Removing the last element in O(1) is only possible when each node knows its previous node.