6 min read
Two Pointers Patterns in LeetCode Problems (Easy)

Group 1: Converging Pointers (From Ends to Middle)

This group introduces the most fundamental use of two pointers: starting at both ends and moving towards the middle. Problems begin with basic swaps and progress to conditional logic and comparisons.

# Problem Link Concept Description of Difficulty Increase
1 Reverse String Link Converging Pointers (Swap) Base case. The most fundamental use of two pointers (left, right) to swap elements in place until they meet.
2 Flipping an Image Link Converging Pointers (Swap) Boring variation. Applies the Reverse String logic to each row of a 2D array, with an added bit-flip step.
3 Reverse Vowels of a String Link Converging Pointers (Conditional Swap) Introduces conditional logic. Pointers must now skip over non-vowel characters before performing a swap.
4 Reverse Only Letters Link Converging Pointers (Conditional Swap) Boring variation of reversing vowels. The exact same logic applies, but the condition checks for letters instead of vowels.
5 Valid Palindrome Link Converging Pointers (Comparison) Switches from swapping to comparing. Pointers converge, comparing characters and skipping non-alphanumeric characters.
6 Two Sum II - Input array is sorted Link Converging Pointers (Search) Applies the converging pattern to a sorted array to find a target sum, adjusting pointers based on whether the current sum is too high or too low.
7 Squares of a Sorted Array Link Converging Pointers (Constructing an Array) A clever twist. Uses converging pointers on the input array to build a new sorted array from the outside in, requiring comparison of absolute values.
8 Valid Palindrome II Link Converging Pointers + Helper Logic Builds on "Valid Palindrome" by allowing the removal of one character, requiring a helper function to check the two resulting sub-problems.

Group 2: Fast & Slow Pointers (Cycle Detection & Linked Lists)

These problems use two pointers moving at different speeds to detect cycles or find midpoints, especially in linked lists and sequences.

# Problem Link Concept Description of Difficulty Increase
9 Middle of the Linked List Link Fast & Slow Pointers Base case for this pattern. Introduces using one pointer moving twice as fast as another to find the midpoint of a sequence.
10 Linked List Cycle Link Fast & Slow Pointers The canonical application of the F&S pattern. If the fast pointer ever meets the slow pointer, a cycle is detected.
11 Happy Number Link Fast & Slow Pointers (Abstract) Conceptual leap. Generalizes cycle detection beyond linked lists to a sequence generated by a function. The F&S logic remains the same.
12 Palindrome Linked List Link Hybrid: F&S + Reversal A multi-step problem. Combines F&S to find the middle, a reversal of the second half, and then a two-pointer comparison.

Group 3: In-place Array Modification (Read & Write Pointers)

This group focuses on modifying arrays in place using separate read and write pointers, often to partition or rearrange elements efficiently.

# Problem Link Concept Description of Difficulty Increase
13 Remove Element Link Two Pointers (Partitioning) Base case for in-place modification. Uses a "write" pointer to fill an array with only the elements that a "read" pointer finds valid.
14 Remove Duplicates from Sorted Array Link Two Pointers (Partitioning) Boring variation of Remove Element. The condition simply changes from checking for a specific value to checking against the previous element.
15 Move Zeroes Link Two Pointers (Partitioning) Builds on the partitioning pattern. All non-zero elements are moved to the front, and the remaining space is then filled with zeros.
16 Sort Array by Parity Link Two Pointers (Partitioning) Boring variation of Move Zeroes. The exact same logic applies, but the condition is now isEven instead of isNotZero.
17 Duplicate Zeros Link Two Pointers (In-place with Lookahead) A much trickier in-place modification. Requires a preliminary pass to count shifts, followed by a right-to-left pass to avoid overwriting elements.

Group 4: Pointers on Two Different Inputs

Problems in this group use pointers on two different arrays or lists, often to merge, compare, or find intersections efficiently.

# Problem Link Concept Description of Difficulty Increase
18 Merge Strings Alternately Link Two Pointers (Two Arrays) The simplest two-input problem. Uses one pointer for each string and builds a new string by taking from each one in turn.
19 Is Subsequence Link Two Pointers (Two Arrays) Classic two-pointer checker. Advances one pointer only when a match is found by the other, checking for an ordered sequence.
20 Long Pressed Name Link Two Pointers (Two Arrays with Grouping) Increases complexity from Is Subsequence by requiring logic to handle groups of identical characters, not just single characters.
21 Intersection of Two Arrays Link Two Pointers (Sorted Arrays) Requires sorting both arrays first. Then, two pointers are used to find common elements efficiently by advancing the smaller of the two values.
22 Intersection of Two Arrays II Link Two Pointers (Sorted Arrays) Boring variation of the previous problem. The logic is nearly identical but is adjusted to handle duplicate elements in the output.
23 Merge Sorted Array Link Three Pointers (In-place Merge) A classic problem using three pointers: two "read" pointers for the input arrays and one "write" pointer to fill the target array from the end.
24 Intersection of Two Linked Lists Link Two Pointers (List Hopping) A clever trick where two pointers traverse their respective lists. When one reaches the end, it "hops" to the head of the other list, guaranteeing they meet at the intersection.

Group 5: Other Variations and Combinations

This group covers other variations and combinations of the two pointers pattern, including nested pointers, sliding windows, and more complex logic.

# Problem Link Concept Description of Difficulty Increase
25 Reverse Words in a String III Link Two Pointers (Nested) Combines patterns. An outer loop/pointer finds word boundaries, and inner converging pointers are used to reverse each word.
26 Backspace String Compare Link Two Pointers (From the End) A complex problem where pointers start from the end of the strings and must "skip" characters when a backspace is encountered.
27 Count Binary Substrings Link Two Pointers (Sliding Window) A clever use of pointers to track the sizes of consecutive groups of 0s and 1s, counting valid substrings based on the minimum of the two group sizes.