Compare ArrayList and LinkedList. Which would you use by default, and why?
Show the explanation
| Operation | ArrayList (resizable array) | LinkedList (doubly linked) |
|---|---|---|
get(i) | O(1) | O(n) — walks the list |
| add at end | amortised O(1) | O(1) |
| add/remove at front or middle | O(n) — shifts elements | O(1) once you are at the node, but finding it is O(n) |
| Memory per element | one reference | node with two extra pointers |
| CPU cache | contiguous, fast | scattered, slow |
Default to `ArrayList`. Random access is constant time, and even inserts in the middle are usually fast in practice because shifting a contiguous array is cheap. LinkedList rarely wins; for a queue or stack, ArrayDeque is faster than both.
List<String> names = new ArrayList<>(); // program to the interface
Deque<Integer> stack = new ArrayDeque<>(); // instead of Stack or LinkedList
stack.push(1); stack.push(2); stack.pop(); // 2Declaring variables as List/Deque (the interface) means you can switch implementations later without changing callers.
Original UniCircle question. Last reviewed 22 Oct 2026.
