LeetCode ยท Two Pointers
Two Pointers Problem Set
Opposite-end convergence, read/write compaction, and fast/slow pointers. These problems turn O(nยฒ) brute force into O(n) scans.
Concept page: Two Pointers
| Difficulty | Pattern | Problem | Key Insight |
|---|---|---|---|
| Easy | converge | LC 125 ยท Valid Palindrome | Two pointers from both ends, skip non-alphanumeric. O(n). |
| Easy | read-write | LC 283 ยท Move Zeroes | Read/write pointer compaction. Move non-zeros forward, fill rest with 0. O(n). |
| Easy | converge | LC 344 ยท Reverse String | Swap from both ends, converge to middle. O(n). |
| Easy | sorted | LC 977 ยท Squares of a Sorted Array | Two pointers from both ends, compare absolute values. O(n). |
| Medium | fast-slow | LC 142 ยท Linked List Cycle II | Detect a meeting point with Floyd's algorithm, then reset one pointer to head to recover the cycle entry. |
| Medium | sorted | LC 167 ยท Two Sum II โ Input Array Is Sorted | Converging pointers on sorted array. O(n). |
| Medium | converge | LC 11 ยท Container With Most Water | Move the shorter wall inward. O(n). |
| Medium | sort+scan | LC 15 ยท 3Sum | Sort + fix one, two-pointer for remaining pair. Skip duplicates. O(nยฒ). |
| Hard | two-pass | LC 42 ยท Trapping Rain Water | Two pointers from both ends tracking left-max and right-max. O(n). |