Two Pointers Pattern: Template + 201 LeetCode Problems
Use the order already in the input to discard half the search space at every step.
- 58 Easy
- 118 Medium
- 25 Hard
- O(n), or O(n log n) when the input has to be sorted first time
What the two pointers pattern is
Two pointers walk the input with two indices whose movement is driven by a comparison rather than by a loop counter, and the whole technique rests on one idea: if the input is ordered, the comparison tells you which pointer cannot possibly be part of the answer, so moving it throws away a whole family of candidates at once. In the converging form the pointers start at opposite ends and step inward — for a target sum, a total that is too small can only be fixed by a larger left value, so the left pointer moves and every pair involving the old left value is eliminated in one step. The same-direction form uses a slow write index behind a fast read index and is how in-place filtering and de-duplication are done without extra memory. The fast-and-slow variant advances one pointer twice as fast as the other, which detects a cycle in a linked list and finds its midpoint without knowing the length.
When to use it
- The input is sorted, or sorting it first does not destroy the answer.
- You are looking for a pair, triple, or a partition point rather than a subarray.
- The problem asks for an in-place rewrite in O(1) extra space.
- A linked list needs a midpoint, a cycle check, or the nth node from the end in one pass.
The two pointers template in Python
The shape, not a solution to any one problem. Adapt the condition and the summary being maintained; the skeleton stays the same across the 201 problems listed below.
def two_sum_sorted(nums, target):
left, right = 0, len(nums) - 1
while left < right:
total = nums[left] + nums[right]
if total == target:
return [left, right]
if total < target:
left += 1 # only a larger left value can reach the target
else:
right -= 1 # only a smaller right value can
return []Complexity characteristics
- Time
- O(n), or O(n log n) when the input has to be sorted first
- Auxiliary space
- O(1)
Each pointer only ever moves in one direction, so between them they take at most n steps and the scan is linear. When the input arrives unsorted the sort dominates and the whole solution is O(n log n) — worth saying out loud, because it is exactly the difference between this and a hash-map solution that stays linear. The extra space is two integers, and the fast-and-slow variant on a linked list is likewise O(1), which is normally the constraint being tested.
All 201 two pointers LeetCode problems
Every problem in the library the two pointers pattern applies to, grouped by LeetCode's own difficulty rating. 169 of the 201 carry a complete Python solution with a worked example and complexity analysis; the rest are listed for completeness, with the LeetCode Premium ones marked.
Easy (58)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 26 | Remove Duplicates from Sorted Array | Easy | Array, Two Pointers |
| 27 | Remove Element | Easy | Array, Two Pointers |
| 28 | Find the Index of the First Occurrence in a String | Easy | Two Pointers, String, String Matching |
| 88 | Merge Sorted Array | Easy | Array, Two Pointers, Sorting |
| 125 | Valid Palindrome | Easy | Two Pointers, String |
| 141 | Linked List Cycle | Easy | Hash Table, Linked List, Two Pointers |
| 160 | Intersection of Two Linked Lists | Easy | Hash Table, Linked List, Two Pointers |
| 202 | Happy Number | Easy | Hash Table, Math, Two Pointers |
| 234 | Palindrome Linked List | Easy | Stack, Recursion, Linked List +1 |
| 283 | Move Zeroes | Easy | Array, Two Pointers |
| 344 | Reverse String | Easy | Two Pointers, String |
| 345 | Reverse Vowels of a String | Easy | Two Pointers, String |
| 349 | Intersection of Two Arrays | Easy | Array, Hash Table, Two Pointers +2 |
| 350 | Intersection of Two Arrays II | Easy | Array, Hash Table, Two Pointers +2 |
| 392 | Is Subsequence | Easy | Two Pointers, String, Dynamic Programming |
| 455 | Assign Cookies | Easy | Greedy, Array, Two Pointers +1 |
| 541 | Reverse String II | Easy | Two Pointers, String |
| 557 | Reverse Words in a String III | Easy | Two Pointers, String |
| 653 | Two Sum IV - Input is a BST | Easy | Tree, Depth-First Search, Breadth-First Search +4 |
| 680 | Valid Palindrome II | Easy | Greedy, Two Pointers, String |
| 696 | Count Binary Substrings | Easy | Two Pointers, String |
| 876 | Middle of the Linked List | Easy | Linked List, Two Pointers |
| 1768 | Merge Strings Alternately | Easy | Two Pointers, String |
| 170 | Two Sum III - Data structure designPremium | Easy | Design, Array, Hash Table +2 |
| 246 | Strobogrammatic NumberPremium | Easy | Hash Table, Two Pointers, String |
| 408 | Valid Word AbbreviationPremium | Easy | Two Pointers, String |
| 821 | Shortest Distance to a Character | Easy | Array, Two Pointers, String |
| 832 | Flipping an Image | Easy | Bit Manipulation, Array, Two Pointers +2 |
| 844 | Backspace String Compare | Easy | Stack, Two Pointers, String +1 |
| 905 | Sort Array By Parity | Easy | Array, Two Pointers, Sorting |
| 917 | Reverse Only Letters | Easy | Two Pointers, String |
| 922 | Sort Array By Parity II | Easy | Array, Two Pointers, Sorting |
| 925 | Long Pressed Name | Easy | Two Pointers, String |
| 942 | DI String Match | Easy | Greedy, Array, Two Pointers +1 |
| 977 | Squares of a Sorted Array | Easy | Array, Two Pointers, Sorting |
| 1089 | Duplicate Zeros | Easy | Array, Two Pointers |
| 1099 | Two Sum Less Than KPremium | Easy | Array, Two Pointers, Binary Search +1 |
| 1332 | Remove Palindromic Subsequences | Easy | Two Pointers, String |
| 1346 | Check If N and Its Double Exist | Easy | Array, Hash Table, Two Pointers +2 |
| 1385 | Find the Distance Value Between Two Arrays | Easy | Array, Two Pointers, Binary Search +1 |
| 1455 | Check If a Word Occurs As a Prefix of Any Word in a Sentence | Easy | Two Pointers, String, String Matching |
| 1826 | Faulty SensorPremium | Easy | Array, Two Pointers |
| 1961 | Check If String Is a Prefix of Array | Easy | Array, Two Pointers, String |
| 2000 | Reverse Prefix of Word | Easy | Stack, Two Pointers, String |
| 2108 | Find First Palindromic String in the Array | Easy | Array, Two Pointers, String |
| 2200 | Find All K-Distant Indices in an Array | Easy | Array, Two Pointers |
| 2367 | Number of Arithmetic Triplets | Easy | Array, Hash Table, Two Pointers +1 |
| 2441 | Largest Positive Integer That Exists With Its Negative | Easy | Array, Hash Table, Two Pointers +1 |
| 2460 | Apply Operations to an Array | Easy | Array, Two Pointers, Simulation |
| 2465 | Number of Distinct Averages | Easy | Array, Hash Table, Two Pointers +1 |
| 2511 | Maximum Enemy Forts That Can Be Captured | Easy | Array, Two Pointers |
| 2540 | Minimum Common Value | Easy | Array, Hash Table, Two Pointers +1 |
| 2562 | Find the Array Concatenation Value | Easy | Array, Two Pointers, Simulation |
| 2570 | Merge Two 2D Arrays by Summing Values | Easy | Array, Hash Table, Two Pointers |
| 2697 | Lexicographically Smallest Palindrome | Easy | Greedy, Two Pointers, String |
| 2824 | Count Pairs Whose Sum is Less than Target | Easy | Array, Two Pointers, Binary Search +1 |
| 2903 | Find Indices With Index and Value Difference I | Easy | Array, Two Pointers |
| 2970 | Count the Number of Incremovable Subarrays I | Easy | Array, Two Pointers, Binary Search +1 |
Medium (118)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 5 | Longest Palindromic Substring | Medium | Two Pointers, String, Dynamic Programming |
| 11 | Container With Most Water | Medium | Greedy, Array, Two Pointers |
| 15 | 3Sum | Medium | Array, Two Pointers, Sorting |
| 16 | 3Sum Closest | Medium | Array, Two Pointers, Sorting |
| 18 | 4Sum | Medium | Array, Two Pointers, Sorting |
| 19 | Remove Nth Node From End of List | Medium | Linked List, Two Pointers |
| 31 | Next Permutation | Medium | Array, Two Pointers |
| 61 | Rotate List | Medium | Linked List, Two Pointers |
| 75 | Sort Colors | Medium | Array, Two Pointers, Sorting |
| 80 | Remove Duplicates from Sorted Array II | Medium | Array, Two Pointers |
| 82 | Remove Duplicates from Sorted List II | Medium | Linked List, Two Pointers |
| 86 | Partition List | Medium | Linked List, Two Pointers |
| 142 | Linked List Cycle II | Medium | Hash Table, Linked List, Two Pointers |
| 143 | Reorder List | Medium | Stack, Recursion, Linked List +1 |
| 148 | Sort List | Medium | Linked List, Two Pointers, Divide and Conquer +2 |
| 151 | Reverse Words in a String | Medium | Two Pointers, String |
| 165 | Compare Version Numbers | Medium | Two Pointers, String |
| 167 | Two Sum II - Input Array Is Sorted | Medium | Array, Two Pointers, Binary Search |
| 189 | Rotate Array | Medium | Array, Math, Two Pointers |
| 287 | Find the Duplicate Number | Medium | Bit Manipulation, Array, Two Pointers +1 |
| 443 | String Compression | Medium | Two Pointers, String |
| 457 | Circular Array Loop | Medium | Array, Hash Table, Two Pointers |
| 475 | Heaters | Medium | Array, Two Pointers, Binary Search +1 |
| 481 | Magical String | Medium | Two Pointers, String |
| 522 | Longest Uncommon Subsequence II | Medium | Array, Hash Table, Two Pointers +2 |
| 524 | Longest Word in Dictionary through Deleting | Medium | Array, Two Pointers, String +1 |
| 532 | K-diff Pairs in an Array | Medium | Array, Hash Table, Two Pointers +2 |
| 556 | Next Greater Element III | Medium | Math, Two Pointers, String |
| 567 | Permutation in String | Medium | Hash Table, Two Pointers, String +1 |
| 581 | Shortest Unsorted Continuous Subarray | Medium | Stack, Greedy, Array +3 |
| 611 | Valid Triangle Number | Medium | Greedy, Array, Two Pointers +2 |
| 633 | Sum of Square Numbers | Medium | Math, Two Pointers, Binary Search |
| 647 | Palindromic Substrings | Medium | Two Pointers, String, Dynamic Programming |
| 658 | Find K Closest Elements | Medium | Array, Two Pointers, Binary Search +3 |
| 763 | Partition Labels | Medium | Greedy, Hash Table, Two Pointers +1 |
| 1679 | Max Number of K-Sum Pairs | Medium | Array, Hash Table, Two Pointers +1 |
| 2095 | Delete the Middle Node of a Linked List | Medium | Linked List, Two Pointers |
| 2130 | Maximum Twin Sum of a Linked List | Medium | Stack, Linked List, Two Pointers |
| 2300 | Successful Pairs of Spells and Potions | Medium | Array, Two Pointers, Binary Search +1 |
| 2462 | Total Cost to Hire K Workers | Medium | Array, Two Pointers, Simulation +1 |
| 161 | One Edit DistancePremium | Medium | Two Pointers, String |
| 186 | Reverse Words in a String IIPremium | Medium | Two Pointers, String |
| 244 | Shortest Word Distance IIPremium | Medium | Design, Array, Hash Table +2 |
| 251 | Flatten 2D VectorPremium | Medium | Design, Array, Two Pointers +1 |
| 253 | Meeting Rooms IIPremium | Medium | Greedy, Array, Two Pointers +3 |
| 259 | 3Sum SmallerPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 277 | Find the CelebrityPremium | Medium | Graph, Two Pointers, Interactive |
| 360 | Sort Transformed ArrayPremium | Medium | Array, Math, Two Pointers +1 |
| 723 | Candy CrushPremium | Medium | Array, Two Pointers, Matrix +1 |
| 777 | Swap Adjacent in LR String | Medium | Two Pointers, String |
| 786 | K-th Smallest Prime Fraction | Medium | Array, Two Pointers, Binary Search +2 |
| 795 | Number of Subarrays with Bounded Maximum | Medium | Array, Two Pointers |
| 809 | Expressive Words | Medium | Array, Two Pointers, String |
| 825 | Friends Of Appropriate Ages | Medium | Array, Two Pointers, Binary Search +1 |
| 826 | Most Profit Assigning Work | Medium | Greedy, Array, Two Pointers +2 |
| 838 | Push Dominoes | Medium | Two Pointers, String, Dynamic Programming |
| 845 | Longest Mountain in Array | Medium | Array, Two Pointers, Dynamic Programming +1 |
| 870 | Advantage Shuffle | Medium | Greedy, Array, Two Pointers +1 |
| 881 | Boats to Save People | Medium | Greedy, Array, Two Pointers +1 |
| 923 | 3Sum With Multiplicity | Medium | Array, Hash Table, Two Pointers +2 |
| 948 | Bag of Tokens | Medium | Greedy, Array, Two Pointers +1 |
| 962 | Maximum Width Ramp | Medium | Stack, Array, Two Pointers +1 |
| 969 | Pancake Sorting | Medium | Greedy, Array, Two Pointers +1 |
| 986 | Interval List Intersections | Medium | Array, Two Pointers, Line Sweep |
| 1023 | Camelcase Matching | Medium | Trie, Array, Two Pointers +2 |
| 1048 | Longest String Chain | Medium | Array, Hash Table, Two Pointers +3 |
| 1055 | Shortest Way to Form StringPremium | Medium | Greedy, Two Pointers, String +1 |
| 1214 | Two Sum BSTsPremium | Medium | Stack, Tree, Depth-First Search +4 |
| 1229 | Meeting SchedulerPremium | Medium | Array, Two Pointers, Sorting |
| 1237 | Find Positive Integer Solution for a Given Equation | Medium | Math, Two Pointers, Binary Search +1 |
| 1265 | Print Immutable Linked List in ReversePremium | Medium | Stack, Recursion, Linked List +1 |
| 1471 | The k Strongest Values in an Array | Medium | Array, Two Pointers, Sorting |
| 1498 | Number of Subsequences That Satisfy the Given Sum Condition | Medium | Array, Two Pointers, Binary Search +1 |
| 1508 | Range Sum of Sorted Subarray Sums | Medium | Array, Two Pointers, Binary Search +2 |
| 1570 | Dot Product of Two Sparse VectorsPremium | Medium | Design, Array, Hash Table +1 |
| 1574 | Shortest Subarray to be Removed to Make Array Sorted | Medium | Stack, Array, Two Pointers +2 |
| 1577 | Number of Ways Where Square of Number Is Equal to Product of Two Numbers | Medium | Array, Hash Table, Math +1 |
| 1616 | Split Two Strings to Make Palindrome | Medium | Two Pointers, String |
| 1634 | Add Two Polynomials Represented as Linked ListsPremium | Medium | Linked List, Math, Two Pointers |
| 1650 | Lowest Common Ancestor of a Binary Tree IIIPremium | Medium | Tree, Hash Table, Two Pointers +1 |
| 1712 | Ways to Split Array Into Three Subarrays | Medium | Array, Two Pointers, Binary Search +1 |
| 1721 | Swapping Nodes in a Linked List | Medium | Linked List, Two Pointers |
| 1750 | Minimum Length of String After Deleting Similar Ends | Medium | Two Pointers, String |
| 1754 | Largest Merge Of Two Strings | Medium | Greedy, Two Pointers, String |
| 1764 | Form Array by Concatenating Subarrays of Another Array | Medium | Greedy, Array, Two Pointers +1 |
| 1813 | Sentence Similarity III | Medium | Array, Two Pointers, String |
| 1850 | Minimum Adjacent Swaps to Reach the Kth Smallest Number | Medium | Greedy, Two Pointers, String |
| 1855 | Maximum Distance Between a Pair of Values | Medium | Array, Two Pointers, Binary Search |
| 1861 | Rotating the Box | Medium | Array, Two Pointers, Matrix |
| 1868 | Product of Two Run-Length Encoded ArraysPremium | Medium | Array, Two Pointers |
| 1877 | Minimize Maximum Pair Sum in Array | Medium | Greedy, Array, Two Pointers +1 |
| 1885 | Count Pairs in Two ArraysPremium | Medium | Array, Two Pointers, Binary Search +1 |
| 1898 | Maximum Number of Removable Characters | Medium | Array, Two Pointers, String +1 |
| 1963 | Minimum Number of Swaps to Make the String Balanced | Medium | Stack, Greedy, Two Pointers +1 |
| 2046 | Sort Linked List Already Sorted Using Absolute ValuesPremium | Medium | Linked List, Two Pointers, Sorting |
| 2105 | Watering Plants II | Medium | Array, Two Pointers, Simulation |
| 2109 | Adding Spaces to a String | Medium | Array, Two Pointers, String +1 |
| 2110 | Number of Smooth Descent Periods of a Stock | Medium | Array, Math, Two Pointers +2 |
| 2149 | Rearrange Array Elements by Sign | Medium | Array, Two Pointers, Simulation |
| 2161 | Partition Array According to Given Pivot | Medium | Array, Two Pointers, Simulation |
| 2330 | Valid Palindrome IVPremium | Medium | Two Pointers, String |
| 2332 | The Latest Time to Catch a Bus | Medium | Array, Two Pointers, Binary Search +1 |
| 2337 | Move Pieces to Obtain a String | Medium | Two Pointers, String |
| 2396 | Strictly Palindromic Number | Medium | Brainteaser, Math, Two Pointers |
| 2406 | Divide Intervals Into Minimum Number of Groups | Medium | Greedy, Array, Two Pointers +3 |
| 2410 | Maximum Matching of Players With Trainers | Medium | Greedy, Array, Two Pointers +1 |
| 2422 | Merge Operations to Turn Array Into a PalindromePremium | Medium | Greedy, Array, Two Pointers |
| 2486 | Append Characters to String to Make Subsequence | Medium | Greedy, Two Pointers, String |
| 2491 | Divide Players Into Teams of Equal Skill | Medium | Array, Hash Table, Two Pointers +1 |
| 2563 | Count the Number of Fair Pairs | Medium | Array, Two Pointers, Binary Search +1 |
| 2576 | Find the Maximum Number of Marked Indices | Medium | Greedy, Array, Two Pointers +2 |
| 2592 | Maximize Greatness of an Array | Medium | Greedy, Array, Two Pointers +1 |
| 2674 | Split a Circular Linked ListPremium | Medium | Linked List, Two Pointers |
| 2825 | Make String a Subsequence Using Cyclic Increments | Medium | Two Pointers, String |
| 2838 | Maximum Coins Heroes Can CollectPremium | Medium | Array, Two Pointers, Binary Search +2 |
| 2856 | Minimum Array Length After Pair Removals | Medium | Greedy, Array, Hash Table +3 |
| 2905 | Find Indices With Index and Value Difference II | Medium | Array, Two Pointers |
| 2938 | Separate Black and White Balls | Medium | Greedy, Two Pointers, String |
Hard (25)
| # | Problem | Difficulty | Topics |
|---|---|---|---|
| 42 | Trapping Rain Water | Hard | Stack, Array, Two Pointers +2 |
| 295 | Find Median from Data Stream | Hard | Design, Two Pointers, Data Stream +2 |
| 321 | Create Maximum Number | Hard | Stack, Greedy, Array +2 |
| 719 | Find K-th Smallest Pair Distance | Hard | Array, Two Pointers, Binary Search +1 |
| 272 | Closest Binary Search Tree Value IIPremium | Hard | Stack, Tree, Depth-First Search +4 |
| 1147 | Longest Chunked Palindrome Decomposition | Hard | Greedy, Two Pointers, String +3 |
| 1163 | Last Substring in Lexicographical Order | Hard | Two Pointers, String |
| 1537 | Get the Maximum Score | Hard | Greedy, Array, Two Pointers +1 |
| 1697 | Checking Existence of Edge Length Limited Paths | Hard | Union Find, Graph, Array +2 |
| 1755 | Closest Subsequence Sum | Hard | Bit Manipulation, Array, Two Pointers +3 |
| 1782 | Count Pairs Of Nodes | Hard | Graph, Array, Hash Table +4 |
| 1793 | Maximum Score of a Good Subarray | Hard | Stack, Array, Two Pointers +2 |
| 1842 | Next Palindrome Using Same DigitsPremium | Hard | Two Pointers, String |
| 2035 | Partition Array Into Two Arrays to Minimize Sum Difference | Hard | Bit Manipulation, Array, Two Pointers +4 |
| 2071 | Maximum Number of Tasks You Can Assign | Hard | Greedy, Queue, Array +4 |
| 2122 | Recover the Original Array | Hard | Array, Hash Table, Two Pointers +2 |
| 2193 | Minimum Number of Moves to Make Palindrome | Hard | Greedy, Binary Indexed Tree, Two Pointers +1 |
| 2234 | Maximum Total Beauty of the Gardens | Hard | Greedy, Array, Two Pointers +4 |
| 2472 | Maximum Number of Non-overlapping Palindrome Substrings | Hard | Greedy, Two Pointers, String +1 |
| 2503 | Maximum Number of Points From Grid Queries | Hard | Breadth-First Search, Union Find, Array +4 |
| 2565 | Subsequence With the Minimum Score | Hard | Two Pointers, String, Binary Search |
| 2604 | Minimum Time to Eat All GrainsPremium | Hard | Array, Two Pointers, Binary Search +1 |
| 2868 | The Wording GamePremium | Hard | Greedy, Array, Math +3 |
| 2911 | Minimum Changes to Make K Semi-palindromes | Hard | Two Pointers, String, Dynamic Programming |
| 2972 | Count the Number of Incremovable Subarrays II | Hard | Array, Two Pointers, Binary Search |
Related patterns
Problems sit in more than one pattern more often than not, and the overlap is where the interesting follow-up questions live.
Two Pointers pattern FAQ
What is the two pointers pattern?
Two pointers walk the input with two indices whose movement is driven by a comparison rather than by a loop counter, and the whole technique rests on one idea: if the input is ordered, the comparison tells you which pointer cannot possibly be part of the answer, so moving it throws away a whole family of candidates at once.
How many LeetCode problems use the two pointers pattern?
This page lists 201 LeetCode problems that the two pointers pattern applies to: 58 Easy, 118 Medium and 25 Hard. 169 of them carry a complete Python solution with complexity analysis.
What is the time complexity of the two pointers pattern?
O(n), or O(n log n) when the input has to be sorted first time and O(1) space. Each pointer only ever moves in one direction, so between them they take at most n steps and the scan is linear. When the input arrives unsorted the sort dominates and the whole solution is O(n log n) — worth saying out loud, because it is exactly the difference between this and a hash-map solution that stays linear. The extra space is two integers, and the fast-and-slow variant on a linked list is likewise O(1), which is normally the constraint being tested.
When should I use the two pointers pattern in an interview?
The input is sorted, or sorting it first does not destroy the answer. You are looking for a pair, triple, or a partition point rather than a subarray.
Which two pointers problem should I start with?
LeetCode 26. Remove Duplicates from Sorted Array is the lowest-numbered Easy problem on this page, which makes it the usual starting point: the technique is visible without the problem's own complications getting in the way.
What patterns are related to two pointers?
Sliding Window, Sorting, Binary Search, Linked List. Problems frequently sit in more than one of these, and the overlap is where the interesting follow-up questions come from.
More ways in: all 22 patterns, the curated study lists, or the full problem list.
Meet the two pointers problem you did not practise
Stealth Interview is a desktop app for macOS and Windows. It reads the coding problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer is saying — while staying invisible to screen sharing.