Back to all patterns

Two Pointers

8 problems • Practice on LeetCode to earn credits

About This Pattern

Master the Two Pointers pattern by practicing these problems organized by difficulty.Focus on the Key Signals - these are the indicators that tell you when to use this pattern.

Easy (3)

Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters.

Key Signals:

Compare from both endsSkip non-alphanumericMove towards center
Intersection of Two Linked Lists

Find the node at which two singly linked lists intersect.

Key Signals:

Find intersectionDifferent lengthsTwo pointers meet
Shortest Word Distance

Given a list of words and two words word1 and word2, return the shortest distance between these two words.

Key Signals:

Track indicesMinimize distanceSingle pass

Medium (4)

Two Sum II Input Array Is Sorted

Given a sorted array, find two numbers that add up to a target.

Key Signals:

Array is sortedPointers from both endsAdjust based on sum
Shortest Unsorted Continuous Subarray

Find the shortest continuous subarray that if sorted, the whole array would be sorted.

Key Signals:

Find boundariesSorting validationSubarray problems
One Edit Distance

Check if two strings are one edit (insert, delete, or replace) away.

Key Signals:

String comparisonEdit distance = 1Case analysis
String Compression

Compress string by replacing consecutive repeating characters with character + count.

Key Signals:

In-place modificationRun-length encodingTwo pointers

Hard (1)

Trapping Rain Water

Given n non-negative integers representing an elevation map, compute how much water it can trap.

Key Signals:

Track max heightsMove from both endsWater level determined by min of max heights