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
DifficultyPatternProblemKey Insight
EasyconvergeLC 125 ยท Valid PalindromeTwo pointers from both ends, skip non-alphanumeric. O(n).
Easyread-writeLC 283 ยท Move ZeroesRead/write pointer compaction. Move non-zeros forward, fill rest with 0. O(n).
EasyconvergeLC 344 ยท Reverse StringSwap from both ends, converge to middle. O(n).
EasysortedLC 977 ยท Squares of a Sorted ArrayTwo pointers from both ends, compare absolute values. O(n).
Mediumfast-slowLC 142 ยท Linked List Cycle IIDetect a meeting point with Floyd's algorithm, then reset one pointer to head to recover the cycle entry.
MediumsortedLC 167 ยท Two Sum II โ€” Input Array Is SortedConverging pointers on sorted array. O(n).
MediumconvergeLC 11 ยท Container With Most WaterMove the shorter wall inward. O(n).
Mediumsort+scanLC 15 ยท 3SumSort + fix one, two-pointer for remaining pair. Skip duplicates. O(nยฒ).
Hardtwo-passLC 42 ยท Trapping Rain WaterTwo pointers from both ends tracking left-max and right-max. O(n).

โ† Back to all LeetCode categories