LinkedList method choice depends on the endpoint, the empty-case contract, and whether removal targets a value or an index.
Java LinkedList method notes
Endpoint pairs
- FIFO: offer or addLast, then poll or pollFirst.
- LIFO: push, then pop; pop throws when empty.
- Read without removing: peekFirst or peekLast returns null when empty.
- Strict endpoint read: getFirst or getLast throws when empty.
- Position: remove(int) returns an element.
- First equal value: remove(Object) returns a boolean.
Costs
Endpoints use O(1) link work. Searching and arbitrary indexing are O(n) in the worst case. A full iterator traversal is O(n). A get(index) loop over the entire list can be O(n²). Each element requires linked-node storage.
Connected lessons
Continue with Java LinkedList: operations, internals and failure cases, Java LinkedList quiz.
Common Mistakes
Null elements make poll and peek results ambiguous. The class does not coordinate access between threads. A shallow copy preserves references to mutable elements.
java
linkedlist-cheatsheet
