Two Pointers

Scan a sorted array or string from both ends at once to find pairs, remove duplicates, or reverse data in O(n).

Two Pointers Practice Problems

Easy

12 problems
  1. 11

    Valid Palindrome

    Check whether a string reads the same forwards and backwards, ignoring case and punctuation.

    easy
  2. 12

    Merge Sorted Array

    Merge two sorted arrays into one sorted array in place.

    easy
  3. 13

    Remove Duplicates from Sorted Array

    Remove duplicate values from a sorted array in place and return the new length.

    easy
  4. 14

    Move Zeroes

    Move every zero in an array to the end while keeping the other numbers in order.

    easy
  5. 21

    Squares of a Sorted Array

    Square every value in a sorted array and return the result sorted, without re-sorting.

    easy
  6. 22

    Reverse String

    Reverse an array of characters in place.

    easy
  7. 23

    Is Subsequence

    Check whether one string is a subsequence of another.

    easy
  8. 24

    Backspace String Compare

    Check whether two strings are equal once # is treated as a backspace in each.

    easy
  9. 200

    Palindrome Linked List

    Check whether a linked list reads the same forwards and backwards.

    easy
  10. 201

    Intersection of Two Arrays II

    Find every value that appears in both arrays, as many times as it appears in both.

    easy
  11. 202

    Reverse Vowels of a String

    Reverse only the vowels of a string, leaving every other character in place.

    easy
  12. 203

    Sort Array By Parity

    Rearrange an array so every even value comes before every odd value.

    easy

Medium

17 problems
  1. 15

    Two Sum II - Input Array Is Sorted

    Find two numbers in a sorted array that add up to a target value.

    medium
  2. 16

    3Sum

    Find every unique triplet of numbers in an array that adds up to zero.

    medium
  3. 17

    Container With Most Water

    Choose two lines from a list of heights that hold the most water between them.

    medium
  4. 18

    Sort Colors

    Sort an array of three distinct values in place in a single pass.

    medium
  5. 19

    4Sum

    Find every unique quadruplet of numbers in an array that adds up to a target value.

    medium
  6. 25

    3Sum Closest

    Find the triplet in an array whose sum is closest to a target value.

    medium
  7. 26

    Remove Duplicates from Sorted Array II

    Remove duplicates in place from a sorted array so each value appears at most twice.

    medium
  8. 27

    Boats to Save People

    Pair the heaviest and lightest people to minimize the number of boats needed within a weight limit.

    medium
  9. 28

    Linked List Cycle

    Detect whether a linked list has a cycle using a fast and slow pointer.

    medium
  10. 29

    Middle of the Linked List

    Find the middle node of a linked list in one pass with a fast and slow pointer.

    medium
  11. 30

    Remove Nth Node From End of List

    Remove the nth node from the end of a linked list in one pass using two pointers.

    medium
  12. 204

    Sort Array By Parity II

    Rearrange an array so every even index holds an even value and every odd index holds an odd value.

    medium
  13. 205

    Rotate Array

    Rotate an array to the right by k steps in place.

    medium
  14. 206

    Reverse Words in a String

    Reverse the order of the words in a sentence while collapsing extra whitespace.

    medium
  15. 207

    Interval List Intersections

    Find every overlapping range between two sorted lists of intervals.

    medium
  16. 208

    Reorder List

    Reorder a linked list by weaving its first half with its reversed second half.

    medium
  17. 209

    Next Permutation

    Rearrange an array into the next lexicographically greater arrangement in place.

    medium

Hard

1 problems
  1. 20

    Trapping Rain Water

    Work out how much water is trapped between bars of varying heights after it rains.

    hard