We're sorry this tutorial wasn't helpful. Please tell us what we can do better.
Linked List Advantages and Disadvantages
Last updated:
What is Linked List?
A linked list stores its elements in separate nodes. Each node holds a value and a reference to the next node, and the program keeps a reference called the head to the first node. The last node's next field is null; an empty list has a null head.
This design trades fast access by position for cheap changes to the shape of the list. The sections below show what you gain and what you pay for that trade.
Terminologies for Linked Lists
Head: the reference to the first node; every traversal starts here.
Node: one element of the list, with a data field and a link field.
Data: the value stored in the node.
Next Pointer: the reference to the following node, or null in the last node.
Advantages of Linked Lists
No fixed capacity: nodes are created one by one as values are added, so you never have to guess a size in advance or copy everything into a bigger array.
Fast insertion and deletion at a known place: adding or removing a node at the head, or right after a node you already hold, only changes one or two references, which is O(1). No other elements move.
No unused slots: the list holds exactly as many nodes as there are values. (Each node still carries the cost of its reference; see the disadvantages.)
Good base for other structures: stacks, queues, deques, adjacency lists and hash-table buckets are easy to build on a linked list.
No need for one large free block: because nodes can live anywhere in memory, a long list does not require a single contiguous region the way a big array does.
Disadvantages of Linked Lists
The same links that make changes cheap also make some things expensive:
No random access: to reach the k-th element you must follow k links from the head, which is O(k). Binary search loses its advantage for the same reason.
Extra memory per element: every node stores at least one reference (two in a doubly linked list) plus an object header in Java, so a list of small values uses much more memory than an array.
Poor cache behaviour: nodes are scattered in memory, so walking a list causes more cache misses than looping over an array, even though both are O(n).
One-way travel in a singly linked list: you cannot step back to the previous node; operations such as deleting the last node need a full walk to find its predecessor.
Memory management depends on references: in C or C++ a removed node must be freed by hand or it leaks. In Java the garbage collector reclaims a node once nothing refers to it, but a node that is still referenced (for example, by a forgotten tail or iterator variable) cannot be reclaimed.
Frequently Asked Questions (FAQ)
Q1. What is the biggest advantage of a linked list over an array?
A linked list can grow and shrink at runtime without copying elements, and it can insert or delete a node in O(1) time once you have a reference to the right position. An array must shift elements and sometimes copy itself into a bigger array.
Q2. What is the biggest disadvantage of a linked list?
There is no random access. To reach the k-th element you must follow k links from the head, which takes O(k) time. Arrays can jump to any index in O(1).
Q3. Why does a linked list use more memory than an array?
Every node stores at least one extra reference (to the next node), and a doubly linked list stores two. On a 64-bit JVM, each reference and each node object header add several bytes, so a list of small integers can use several times more memory than an int[] of the same size.
Q4. Why are linked lists less cache-friendly than arrays?
The CPU loads memory in blocks into its cache. Array elements sit next to each other, so one load brings in many elements. Linked list nodes can be scattered across memory, so each step may need a new memory load. This makes traversing a linked list slower in practice, even though both are O(n).
Q5. Write Java code to delete a node when you only have a reference to that node.
Copy the next node's value into the current node and skip the next node. This works for any node except the last one:
DeleteGivenNode.java
public class DeleteGivenNode {
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
}
}
static void deleteNode(Node node) {
node.data = node.next.data; // copy the next value here
node.next = node.next.next; // unlink the next node
}
}
This trick shows the O(1) delete advantage of linked lists, but it cannot be used on the tail node.
Q6. Can binary search be used on a sorted linked list?
Not efficiently. Each step of binary search needs to jump to the middle, which takes O(n) on a linked list, so the advantage of binary search is lost. A skip list or a balanced binary search tree is used when you need fast search on linked data.
Q7. When is a linked list a good choice in a real application?
Implementing queues and stacks that change size often.
An LRU cache, where items move to the front on every access.
Undo history and browser history that move forward and back.
Chaining in a hash table, where colliding keys share a bucket.
Q8. Write a Java program that shows the O(1) insert advantage at the head.
HeadInsert.java
public class HeadInsert {
static class Node {
int data;
Node next;
Node(int data) { this.data = data; }
}
static Node push(Node head, int value) {
Node node = new Node(value);
node.next = head;
return node;
}
public static void main(String[] args) {
Node head = null;
for (int i = 1; i <= 5; i++) {
head = push(head, i); // O(1) each time
}
for (Node n = head; n != null; n = n.next) {
System.out.print(n.data + " "); // 5 4 3 2 1
}
}
}
Q9. Is reversing a linked list harder than reversing an array?
Both are O(n). An array is reversed by swapping values from both ends. A singly linked list cannot move backwards, so you reverse it by changing every next reference while walking forward, which needs careful handling of three pointers: previous, current and next.
Q10. Should you prefer java.util.LinkedList or ArrayList in Java?
In most programs ArrayList is faster, because of better cache behaviour and less memory use. Choose LinkedList only when you add and remove at both ends very often; even then, ArrayDeque is usually faster for queue and stack use.