Algorithms

LinkedList

๐Ÿ“‹ Quick Reference

PropertyValue
TypeDoubly-linked node sequence
AccessO(n) - must traverse from head/tail
Insert/Delete at EndsO(1)
Insert/Delete at MiddleO(1) with iterator, O(n) to find position
MemoryExtra overhead per node (prev + next pointers)
Best ForFrequent insertions/deletions at known positions
One-liner: Doubly-linked list with O(1) add/remove at ends, but O(n) random access.

๐ŸŽฎ Interactive Visualizer

Watch how LinkedList manages node connections for insertions and deletions:

Loading visualizer...
Try these operations:
  1. Add at head/tail - see O(1) pointer updates
  2. Insert in middle - observe node linking
  3. Remove a node - watch pointer rewiring
  4. Traverse the list - see the step-by-step access

๐Ÿ”ง Key Operations

Creating

JAVA(8 lines)
Code
Loading syntax highlighter...

Adding Elements

JAVA(8 lines)
Code
Loading syntax highlighter...

Accessing

JAVA(7 lines)
Code
Loading syntax highlighter...

Removing

JAVA(8 lines)
Code
Loading syntax highlighter...

Iteration

JAVA(16 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationTimeNotes
addFirst/LastO(1)Just update pointers
removeFirst/LastO(1)Just update pointers
get(i)O(n)Must traverse from head or tail
add(i, e)O(n)Find position + O(1) insert
remove(i)O(n)Find position + O(1) remove
contains(e)O(n)Linear search
size()O(1)Stored as field

Memory Layout

Head โ†’ [prev|data|next] โŸท [prev|data|next] โŸท [prev|data|next] โ† Tail
        nullโ†โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ†’ null

Each node: ~24 bytes overhead (object header + 2 references)
vs ArrayList: ~4 bytes per element (just the reference)

โœ… When to Use LinkedList

Good Use Cases

  • Queue/Deque operations - frequent add/remove at both ends
  • Iterator-based modifications - O(1) insert/delete during iteration
  • Large elements - moving pointers is cheaper than copying
  • Frequent splicing - merging lists is O(1)

Avoid When

  • Random access needed - use ArrayList instead
  • Memory is tight - node overhead is significant
  • Cache performance matters - non-contiguous memory
  • Small elements - overhead dominates actual data

๐Ÿ”„ LinkedList as Queue/Stack

JAVA(16 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Using get() in Loops

JAVA(9 lines)
Code
Loading syntax highlighter...

2. Wrong Data Structure Choice

JAVA(7 lines)
Code
Loading syntax highlighter...

3. Ignoring ArrayDeque Alternative

JAVA(5 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: When would you use LinkedList over ArrayList?
"

When you need frequent insertions/deletions at both ends (as Deque), or when modifying during iteration using ListIterator. In practice, ArrayList is usually better due to cache locality and lower memory overhead.

Q: What's the time complexity of get(n/2)?
"

O(n), specifically O(n/2). Java's LinkedList optimizes by starting from the closer end (head or tail), but it's still linear. For index n/2, it traverses n/2 nodes.

Q: How does LinkedList implement List and Deque?
"

It's a doubly-linked list where each node has prev/next references. Head and tail references enable O(1) operations at both ends, satisfying Deque. The List interface is satisfied by traversing to any index.

Q: ArrayList vs LinkedList for a queue?
"
Neither is ideal. Use ArrayDeque - it has O(1) operations at both ends like LinkedList, but with ArrayList's cache locality and lower memory overhead.

๐Ÿ“š Series Navigation


Visualizer: LinkedList from @tomaszjarosz/react-visualizers