LinkedList
LinkedList is a doubly linked list that implements List and Deque. Each element sits in a node with pointers to its neighbours. Adding at the ends is cheap. Getting by index is not.
Detailed Explanation
An ArrayList stores elements in one array. A LinkedList stores them in nodes: value, previous, next. Inserting at the head or tail updates a few pointers and does not move the other elements. Inserting in the middle still requires walking to that node. get(i) always walks from the nearer end, so it is O(n).
That is why a loop written as for (int i = 0; i < list.size(); i++) list.get(i) is O(n²) on a LinkedList and O(n) on an ArrayList. The enhanced for and an Iterator walk node to node and stay O(n). If you need indexes, you picked the wrong list.
LinkedList also implements Deque: addFirst, addLast, removeFirst, removeLast, peek. It can be a queue or a stack. ArrayDeque is usually faster for those jobs because it is a resizable ring buffer without per-element nodes. Reach for LinkedList when you need both list indexes (rarely) and frequent end inserts, or when you must store null (ArrayDeque forbids null).
Each node is an object with a header and two references. A million boxed integers in a LinkedList waste more memory than the same million in an ArrayList, and they hurt the cache: walking the list jumps around the heap. That is the main reason LinkedList is the wrong default.
You reach for it when you are teaching lists, when you need null elements in a deque, or when an API already returns one. You reach for ArrayList for random access and append. You reach for ArrayDeque for queues and stacks. You reach for CopyOnWriteArrayList or a concurrent queue when threads share the structure.
The cost of choosing LinkedList because "insert is O(1)" is that insert is O(1) only once you have the node. list.add(index, x) is O(n). Profile before you switch away from ArrayList; the switch is almost never a win for ordinary application lists.
Code Examples
import java.util.LinkedList;
public class Ends {
public static void main(String[] args) {
LinkedList<String> names = new LinkedList<>();
names.addLast("bo");
names.addFirst("ana");
names.addLast("cy");
System.out.println(names); // prints: [ana, bo, cy]
System.out.println(names.removeFirst()); // prints: ana
System.out.println(names.getLast()); // prints: cy
}
}import java.util.Iterator;
import java.util.LinkedList;
public class Walk {
public static void main(String[] args) {
LinkedList<Integer> xs = new LinkedList<>();
xs.add(1);
xs.add(2);
xs.add(3);
int sum = 0;
Iterator<Integer> it = xs.iterator(); // O(n), not get(i)
while (it.hasNext()) {
sum += it.next();
}
System.out.println(sum); // prints: 6
}
}Common Gotchas
`get(i)` in a counted loop is O(n²)
use the enhanced `for` or an iterator.
`ArrayDeque` is a better deque
use `LinkedList` when you need `List` plus `null`s, not as the default queue.
`LinkedList` is not thread-safe
wrap it or use a concurrent collection.
Each element is a node object
memory and cache cost dominate any pointer-update win on small lists.