Java Notes
Learn Java LinkedList — doubly-linked node structure, when LinkedList outperforms ArrayList, using it as a Queue and Deque, and common operations with clear examples.
LinkedList stores elements in nodes, where each node holds a value and pointers to the next and previous nodes. Unlike ArrayList which stores everything in one continuous array, LinkedList elements can be anywhere in memory — connected only through pointers.
This structure means inserting or removing at the front or middle is fast (just reroute pointers), but accessing a specific index is slow (you must follow the chain from the start).
Creating and Using LinkedList
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
list.addFirst("Apple")— inserts at the front in O(1). Very fast.list.add(2, "Blueberry")— insert at index 2: LinkedList must walk to that position first (O(n)).list.getFirst()andlist.getLast()— O(1) access to both ends.list.get(1)— O(n) because it walks from the head.
LinkedList as a Queue (FIFO)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
offer() adds to the tail. poll() removes from the head. This is FIFO — first in, first out. Perfect for queuing tasks, requests, or events.
LinkedList as a Deque (Both Ends)
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Deque (double-ended queue) allows adding and removing from both ends. Useful for sliding window problems, undo/redo stacks, and browser history.
LinkedList vs ArrayList — When to Choose
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
| Operation | ArrayList | LinkedList |
|---|---|---|
| Get by index | O(1) — fast | O(n) — slow |
| Add/remove at end | O(1) | O(1) |
| Add/remove at front | O(n) — shifts all | O(1) — pointer change |
| Add/remove at middle | O(n) — shifts | O(n) — find position first |
| Memory per element | Less | More (stores prev/next pointers) |
Choose ArrayList when: you mostly read by index, or add to the end. Choose LinkedList when: you frequently add/remove from both ends, or use it as a queue/deque.
In most real programs, ArrayList is faster because modern CPUs cache contiguous memory very efficiently. Use LinkedList specifically for queue/deque behaviour.
Common Mistake: Using LinkedList for Random Access
If you find yourself using list.get(i) in a loop with LinkedList, switch to ArrayList.
Practice Task
Task: Simulate a printer queue using LinkedList as a Queue<String>. Add 4 print jobs. Print the total number in the queue. Process (poll) each job one by one and print "Printing: [job]". After each print, show how many jobs remain.
Hint: Use queue.size() before and after polling. The loop condition is !queue.isEmpty().
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: poll() removes and returns the first item each time — the queue shrinks. peek() would let you look at the front without removing. Try replacing Queue<String> with Deque<String> and add addFirst() — you can jump a job to the front of the queue.
Continue the lesson path