Advanced Topic
Medium Linked List Algorithms
Master advanced linked list techniques including cycle detection with Floyd's algorithm, finding cycle start nodes, locating middle elements, merging sorted lists, and removing nodes from the end in a single pass.
1. Properties
Two-Pointer Technique: Many advanced LL algorithms use slow/fast pointers moving at different speeds to achieve O(n) time with O(1) space.
Dummy Head Node: Simplifies edge cases when modifying the head pointer (adding/removing head node).
Cycle Detection: Floyd's algorithm detects cycles using tortoise (slow) and hare (fast) pointers without extra space.
Single Pass Solutions: All algorithms here solve their problem in one pass through the list.
2. Key Operations
3. Complexity Analysis
4. Visual: Two-Pointer Technique
Slow & Fast Pointers (Find Middle):
HEAD
│
▼
┌───┬───┐ ┌───┬───┐ ┌───┬───┐ ┌───┬───┐ ┌───┬───┐
│ 10│ ──►│ 20│ ──►│ 30│ ──►│ 40│ ──►│ 50│ ──► null
└───┴───┘ └───┴───┘ └───┴───┘ └───┴───┘ └───┴───┘
S S S F F
(1) (2) (3) (4) (5)
Fast moves 2 steps, Slow moves 1 step
When Fast reaches end → Slow is at middle
Cycle Detection:
HEAD
│
▼
┌───┬───┐ ┌───┬───┐ ┌───┬───┐
│ 10│ ──►│ 20│ ──►│ 30│ ──►········
└───┴───┘ └───┴───┘ └───┴───┘ │
▲ │
│ │
└───────────────────────────┘
(cycle back to index 0)
Slow: 0 → 1 → 2 → 3 → 0 → 1 → ...
Fast: 0 → 2 → 0 → 2 → 0 → 2 → ...
↑
They meet here!