Two Pointers
The two-pointer technique walks two indices through a sequence — from both ends inward, or one ahead of the other — to solve a problem in a single pass and O(1) extra space. Reach for it on sorted arrays or strings when you're looking for a pair, a triplet, or a partition and a brute-force double loop would be O(n²).
24 LeetCode problems solved with the Two Pointers pattern. Practice them with spaced repetition so the pattern sticks.
- 3SumMEDIUM · O(n^2)
- 4SumMEDIUM · O(n^3)
- Concatenation of ArrayEASY · O(n)
- Container With Most WaterMEDIUM · O(n)
- Is SubsequenceEASY · O(n)
- Merge Sorted ArrayEASY · O(m + n)
- Merge Strings AlternatelyEASY · O(n + m)
- Move ZeroesEASY · O(n)
- Next PermutationMEDIUM · O(n)
- Remove Duplicates from Sorted ArrayEASY · O(n)
- Remove Duplicates from Sorted Array IIMEDIUM · O(n)
- Remove ElementEASY · O(n)
- Reverse StringEASY · O(n)
- Reverse Vowels of a StringEASY · O(n)
- Reverse Words in a StringMEDIUM · O(n)
- Rotate ArrayMEDIUM · O(n)
- Sort ColorsMEDIUM · O(n)
- Squares of a Sorted ArrayEASY · O(n)
- String CompressionMEDIUM · O(n)
- Summary RangesEASY · O(n)
- Trapping Rain WaterHARD · O(n)
- Two Sum II - Input Array Is SortedMEDIUM · O(n)
- Valid PalindromeEASY · O(n)
- Valid Palindrome IIEASY · O(n)
See the full solution
The complete approach, reference solutions in 5 languages, and a step-by-step visualization — then add this problem to your spaced-repetition schedule.
Start free →