7. Two Pointers

Opposite ends, same direction, and merging — two indexes instead of nested loops.

Your progress0 of 13
6easy
6medium
1hard

Understand the topic

Two pointers means using two indexes instead of a nested loop. It shows up in three shapes: pointers at both ends moving inward (sorted pairs, palindromes), a slow "write" pointer and a fast "read" pointer moving the same way (removing items in place), and one pointer per array (merging).

The key question every time: after looking at the two pointed values, which pointer can I safely move, and why can't I miss the answer by moving it?

#ProblemPatternLevel
1 Remove Duplicates from Sorted Array #26 Read/write pointers Easy LeetCode ↗
2 Remove Element #27 Read/write pointers Easy LeetCode ↗
3 Move Zeroes #283 Read/write pointers Easy LeetCode ↗
4 Valid Palindrome #125 Opposite ends Easy LeetCode ↗
5 Squares of a Sorted Array #977 Opposite ends, fill from back Easy LeetCode ↗
6 Is Subsequence #392 Same direction Easy LeetCode ↗
7 Two Sum II - Input Array Is Sorted #167 Opposite ends Medium LeetCode ↗
8 3Sum #15 Sort + two pointers Medium LeetCode ↗
9 4Sum #18 Sort + two pointers Medium LeetCode ↗
10 Container With Most Water #11 Opposite ends, move the shorter Medium LeetCode ↗
11 Remove Duplicates from Sorted Array II #80 Read/write pointers Medium LeetCode ↗
12 Boats to Save People #881 Sort + opposite ends Medium LeetCode ↗
13 Trapping Rain Water #42 Opposite ends with max heights Hard LeetCode ↗

Work top to bottom — each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.