Java LinkedList
In this page:
Introduction to LinkedList
LinkedList implements List using a doubly-linked chain of nodes rather than a contiguous array, so each element only needs to know its neighbors, not a fixed memory block.
Example: Introduction to LinkedList
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>(); // chain of nodes
list.add("a");
list.add("b");
System.out.println(list);
}
}
Login to try C/C++/Java/PHP code in the editor
LinkedList as a Queue / Deque
Because it also implements Deque, LinkedList can be used directly as a queue (FIFO, via offer()/poll()) or a stack (LIFO, via push()/pop()) without needing a separate data structure.
Example: LinkedList as a Queue / Deque
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> queue = new LinkedList<>();
queue.offer(1); // FIFO
queue.offer(2);
System.out.println(queue.poll());
LinkedList<Integer> stack = new LinkedList<>();
stack.push(1); // LIFO
stack.push(2);
System.out.println(stack.pop());
}
}
Login to try C/C++/Java/PHP code in the editor
Inserting and Removing Nodes
Inserting or removing a node is fast (O(1)) once you already have a reference to that position, since it's just a matter of relinking neighboring pointers — no shifting of other elements required.
Example: Inserting and Removing Nodes
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("a");
list.add("c");
list.add(1, "b"); // fast insert once at that position
System.out.println(list);
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over LinkedList
Iterating a LinkedList works the same way syntactically as an ArrayList (for-each or Iterator), but random access by index is slow (O(n)) since the list has to walk node-by-node from one end to reach a given position.
Example: Iterating over LinkedList
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("a");
list.add("b");
for (String item : list) { // same syntax as ArrayList
System.out.println(item);
}
}
}
Login to try C/C++/Java/PHP code in the editor
ArrayList vs LinkedList
Choose ArrayList when you mostly read by index; choose LinkedList when you mostly insert/remove at the ends or in the middle via an iterator — picking the wrong one for your access pattern can hurt performance significantly.
Example: ArrayList vs LinkedList
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>(); // best for index reads
LinkedList<Integer> linkedList = new LinkedList<>(); // best for insert/remove at ends
arrayList.add(1);
linkedList.addFirst(1);
System.out.println(arrayList.get(0));
System.out.println(linkedList.getFirst());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting