Java LinkedList
LinkedList is another implementation of the List interface. It offers almost the same methods as ArrayList, but stores its elements in a completely different way — and it also works as a queue and a stack.
How it differs from ArrayList
- An
ArrayListkeeps elements in one contiguous array. Readingget(i)is instant, but inserting at the front means shifting every other element. - A
LinkedListstores each element in a separate node that links to the previous and next node (a doubly linked list). Adding or removing at either end is instant, butget(i)has to walk through the nodes one by one.
ArrayList: [ A | B | C | D ]
LinkedList: null <- A <-> B <-> C <-> D -> null
(first) (last)Using it as a List
Because both implement List, you can use the familiar methods:
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> stops = new LinkedList<>();
stops.add("Central");
stops.add("Museum");
stops.add("Harbour");
stops.add(1, "Market");
System.out.println(stops);
System.out.println(stops.get(2) + ", size " + stops.size());
stops.remove("Museum");
System.out.println(stops);
}
}[Central, Market, Museum, Harbour]
Museum, size 4
[Central, Market, Harbour]
Methods for both ends
LinkedList adds methods that work on the first and last element:
| Method | Description |
|---|---|
addFirst(e) / addLast(e) |
insert at the beginning / end |
getFirst() / getLast() |
read the first / last element |
removeFirst() / removeLast() |
remove and return the first / last element |
peekFirst() / peekLast() |
like get, but return null when empty |
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<Integer> nums = new LinkedList<>();
nums.add(20);
nums.addFirst(10);
nums.addLast(30);
System.out.println(nums);
System.out.println("first=" + nums.getFirst() + " last=" + nums.getLast());
nums.removeFirst();
nums.removeLast();
System.out.println(nums);
}
}[10, 20, 30]
first=10 last=30
[20]
As a queue (first in, first out)
LinkedList implements the Queue and Deque interfaces. A queue serves items in the order they arrived — like people waiting at a counter:
import java.util.LinkedList;
import java.util.Queue;
class Main {
public static void main(String[] args) {
Queue<String> printJobs = new LinkedList<>();
printJobs.offer("report.pdf");
printJobs.offer("photo.png");
printJobs.offer("invoice.docx");
System.out.println("Next up: " + printJobs.peek());
while (!printJobs.isEmpty()) {
System.out.println("Printing " + printJobs.poll());
}
System.out.println("Queue empty, poll gives: " + printJobs.poll());
}
}Next up: report.pdf
Printing report.pdf
Printing photo.png
Printing invoice.docx
Queue empty, poll gives: null
As a stack (last in, first out)
Through the Deque interface, push and pop work on the front, so the last item added is the first one removed — like an undo history:
import java.util.Deque;
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
Deque<String> undo = new LinkedList<>();
undo.push("type 'Hello'");
undo.push("make bold");
undo.push("insert image");
System.out.println("Undo: " + undo.pop());
System.out.println("Undo: " + undo.pop());
System.out.println("Remaining: " + undo);
}
}Undo: insert image
Undo: make bold
Remaining: [type 'Hello']
Tip: For plain queues and stacks,
ArrayDequeis usually faster thanLinkedListand is the recommended choice. UseLinkedListwhen you also needListfeatures ornullelements.
Which list should I use?
| Operation | ArrayList | LinkedList |
|---|---|---|
get(i) / set(i, e) |
fast | slow (walks the nodes) |
| add/remove at the end | fast | fast |
| add/remove at the front | slow (shifts elements) | fast |
| memory per element | small | larger (two links per node) |
In practice ArrayList is the right default for almost everything. Reach for LinkedList (or ArrayDeque) when you mainly add and remove at the ends.
Test yourself
Which operation is typically slower on a LinkedList than on an ArrayList?
- addFirst
- removeFirst
- get(i) by index
- addLast
In a Queue, which method removes and returns the head, or returns null if empty?
- peek()
- poll()
- push()
- get()
After push("a"); push("b"); push("c"); on a Deque, what does pop() return?
- a
- b
- c
- null