LinkedList
Creating a LinkedList
import java.util.LinkedList;
import java.util.List;
List<String> names = new LinkedList<>();
names.add("Ravi");
names.add("Meera");
names.add("Ken");
System.out.println(names);[Ravi, Meera, Ken]
How the Internal Structure Shapes Performance
Every element in a LinkedList is wrapped in a node object that stores the value plus two pointers (previous and next). Adding or removing an element at either end of the list is a constant-time operation — O(1) — because it only involves rewiring a couple of pointers, with no shifting of other elements required.
i the way an array can. Calling get(i) or set(i, value) must walk the chain of nodes one link at a time, starting from whichever end is closer, until it reaches the target position. For a list of a million elements, repeatedly calling get(i) in a loop can be dramatically slower than the same loop over an ArrayList.Fast at the ends, slow in the middle by index
import java.util.LinkedList; LinkedList<Integer> numbers = new LinkedList<>(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.addFirst(5); // O(1) — no shifting needed numbers.addLast(40); // O(1) System.out.println(numbers); System.out.println(numbers.get(2)); // O(n) — must traverse from an end
[5, 10, 20, 30, 40] 20
LinkedList Implements List AND Deque
ArrayList vs LinkedList
Aspect | ArrayList | LinkedList |
Backing structure | Resizable array | Doubly-linked list of nodes |
Access by index (get/set) | O(1) | O(n) — must traverse |
Insert/remove at the ends | O(1) amortized | O(1) |
Insert/remove in the middle | O(n) — elements must shift | O(n) to find the spot, O(1) once there |
Memory overhead per element | Low — just the array slot | Higher — each node stores 2 extra references |
Implements | List, RandomAccess | List, Deque, Queue |
get(i) in a loop is O(n²).Backed by a doubly-linked list of nodes, not an array
O(1) insertion and removal at both ends
O(n) access by index — avoid get(i) in tight loops
Implements both List and Deque, so it can act as a stack or queue
ArrayList remains the better general-purpose default