Tags

Two Pointers

Two Pointers

2 lessons
16 problems
5 code snippets
2 question banks
18 community items

two-pointers

Algorithms

2 lessons

Linked List Algorithms

Intermediate

55 min

2 prereqs

Reversing a singly linked list is a four-line iteration with three pointers (`prev`, `curr`, `next`), and yet roughly a third of candidates implement it incorrectly under interview pressure. The reason is that linked-list problems give you no random access: every operation has to be expressed as careful, ordered pointer rewrites, and one missed assignment loses the rest of the list forever. **Linked List Algorithms** is the lesson where pointer manipulation becomes a reliable skill. You will reverse a list iteratively and recursively, then extend the technique to reverse in groups of `k`. You will use Floyd's fast and slow pointers to detect cycles, find the cycle start, locate the middle element, and check whether a list is a palindrome. You will merge two sorted lists, then merge `k` sorted lists with a min-heap. The lesson also covers removing the nth node from the end, finding the intersection of two lists, deep-copying a list with random pointers, and the dummy-node trick that eliminates head-edge cases entirely. In **Linked Lists (Singly)**, you saw how nodes and `next` pointers form a list and why insertion at a known position is `O(1)`. **Two Pointers (Intro)** introduced fast and slow indices on arrays; this lesson reuses the same idea on pointer-linked nodes. Next, **Tree Algorithms** generalizes pointer-and-recursion thinking to branching structures.

Not Started

0%

Algorithms
Singly Linked List
Fast/Slow Pointers
Two Pointers
Cycle Detection
Patterns
Intermediate
Premium

Two Pointers (Intro)

Free
Beginner

55 min

1 prereq

Given a sorted array and a target sum, the brute-force "check every pair" approach is `O(n^2)`. With two indices walking inward from opposite ends, the same problem drops to `O(n)`: if the current pair sums too low, move the left pointer right; if it sums too high, move the right pointer left. **Two Pointers (Intro)** turns that insight into a reusable pattern with two main shapes. The opposite-direction variant has pointers converge from both ends and powers problems like pair sum, palindrome check, reverse in place, and container with most water. The same-direction (fast and slow) variant has both pointers walk forward at different speeds and powers in-place filtering problems like remove duplicates from a sorted array, move zeros to the end, and the linked-list cycle detection you will revisit later. In **Arrays & Strings**, you learned how a single index walks across an array. This lesson coordinates two indices in a way that exploits sorted order or a structural invariant to skip work an inner loop would otherwise repeat. Next, **Sliding Window (Intro)** generalizes the same-direction pointer pair into an expanding and shrinking range that solves contiguous-subarray problems.

Not Started

0%

Algorithms
Two Pointers
Arrays
Fast/Slow Pointers
Patterns
Time Complexity
Beginner
Free

Practice Problems

16 problems

Is Subsequence

Free
Not Started
Easy

Determine if string s is a subsequence of string t by checking if all characters of s appear in t in the same order.

Two Pointers
Strings
Subsequence
Beginner

332

4

Merge Sorted Array

Free
Not Started
Easy

Merge two sorted arrays into one sorted array in-place, using the extra space at the end of the first array.

Arrays
Two Pointers
Sorting
In-Place
Beginner

259

8

Merge Two Sorted Lists

Free
Not Started
Easy

Merge two sorted linked lists into one sorted linked list by splicing the nodes together.

Singly Linked List
Two Pointers
Recursion
Beginner

487

4

Remove Duplicates from Sorted Array

Free
Not Started
Easy

Remove duplicates from a sorted array in-place so each element appears only once, and return the new length.

Arrays
Two Pointers
In-Place
Beginner

685

6

Remove Element

Free
Not Started
Easy

Remove all occurrences of a given value from an array in-place and return the new length.

Arrays
Two Pointers
In-Place
Beginner

936

26

Valid Palindrome

Free
Not Started
Easy

Determine if a string is a palindrome, considering only alphanumeric characters and ignoring cases.

Two Pointers
Strings
Palindrome
Beginner

284

5

Trapping Rain Water

Not Started
Hard

Given an elevation map, compute how much water it can trap after raining.

Two Pointers
Arrays
Trapping Rain Water
Monotonic Stack
Advanced

335

2

Container With Most Water

Free
Not Started
Medium

Find two lines that together with the x-axis form a container holding the most water.

Two Pointers
Arrays
Greedy
Intermediate

628

15

Partition List

Free
Not Started
Medium

Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x, preserving the original relative order.

Singly Linked List
Two Pointers
Intermediate

771

11

Remove Duplicates from Sorted Array II

Not Started
Medium

Remove duplicates from a sorted array in-place so each element appears at most twice, using a two-pointer technique.

Arrays
Two Pointers
In-Place
Intermediate

878

23

Remove Nth Node From End of List

Free
Not Started
Medium

Given the head of a linked list, remove the nth node from the end of the list and return its head.

Singly Linked List
Two Pointers
Intermediate

171

2

Reorder List

Free
Not Started
Medium

Reorder a linked list from L0 -> L1 -> ... -> Ln to L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> ... in-place.

Singly Linked List
Fast/Slow Pointers
Two Pointers
Intermediate

227

3

Reverse Words in a String

Not Started
Medium

Reverse the order of words in a string, handling leading/trailing spaces and multiple spaces between words.

Strings
String Manipulation
Two Pointers
Intermediate

401

11

Sort Colors

Not Started
Medium

Sort an array containing only 0s, 1s, and 2s in-place using a single pass (Dutch National Flag problem).

Two Pointers
Arrays
Dutch National Flag
Sorting
Intermediate

1k

19

3Sum

Free
Not Started
Medium

Find all unique triplets in an array that sum to zero, avoiding duplicate triplets in the result.

Two Pointers
Arrays
Sorting
Intermediate

508

14

Two Sum II - Input Array Is Sorted

Free
Not Started
Medium

Given a 1-indexed sorted array, find two numbers that add up to a target and return their indices.

Two Pointers
Arrays
Binary Search
Intermediate

1k

14

Code Snippets

5 snippets
Code Snippet

Binary Search Template

Binary search is the most asked algorithm in interviews and the easiest to get wrong: off-by-one bugs, infinite loops, integer overflow on the midpoint. This snippet covers the iterative bounds template that always terminates, a recursive variant for contrast, and a found-or-insertion-point version that returns where the value would go if absent. The same skeleton powers the lower-bound and upper-bound variants in the next entry.

JavaScript
algorithms
binary-search
code-template
two-pointers

967

17

Easy
Code Snippet

Two-Pointer Template

The two-pointer technique is the linear-time answer to many sorted-array problems: pair sums, palindrome checks, in-place reversal, partition. This snippet covers the opposite-ends sweep for sorted-pair targets, the reverse-in-place pattern, and the slow-fast pointer used for in-place mutation. Each fits in 10 lines and runs in O(n) time, O(1) extra space.

JavaScript
algorithms
two-pointers
code-template
arrays

418

3

Easy
Code Snippet

Sliding Window Template

Sliding window is the linear-time alternative to nested loops for substring-and-subarray problems. This snippet covers the fixed-size window for max-sum-of-k, the variable-size window for longest-substring-with-condition, and the at-most-K shrinkable form that handles 'at most / exactly K distinct' problems with one trick.

JavaScript
algorithms
sliding-window
code-template
two-pointers

994

12

Easy
Code Snippet

Lower-Bound and Upper-Bound Binary Search

Lower-bound and upper-bound binary search are the two primitives every other range query depends on. Lower-bound returns the first index where a value could be inserted; upper-bound returns the first index strictly greater than the value. This snippet covers both forms, then composes them to count occurrences in a sorted array in O(log n).

JavaScript
algorithms
binary-search
code-template
two-pointers

1.1k

34

Medium
Code Snippet

Python Two-Pointer Template

The two-pointer pattern walks two indices through a sorted array, moving them inward (or together) based on a comparison. It turns many naive O(n^2) problems into O(n) sweeps. This entry covers the inward-sweep template (two-sum sorted), the same-direction template (remove duplicates in place), and a 3-sum builder that uses two pointers as the inner loop.

Python
two-pointers
algorithms
code-template
py-standard-library

930

4

Easy

Community

18 items
Article

Sliding Window Technique

What sliding window actually optimises (state reuse, not just same-direction pointers), the fixed vs variable templates, four canonical problems, and when the technique is the wrong tool.

sliding-window
two-pointers
array-manipulation-patterns
subarray-substring
interview-prep

827

9

May 16, 2026

by @sophiesharma

Problem
Medium
Free

Boats to Save People

Given person weights, a boat capacity, and a 2-person-per-boat limit, find the minimum number of boats.

greedy
two-pointers
sorting

179

4

May 13, 2026

by @chloesaeed

Problem
Medium
Free

Odd Even Linked List

Reorder a singly-linked list so all odd-positioned nodes come before even-positioned ones, in O(n) time and O(1) space using two interleaved sub-lists.

linked-list
two-pointers

512

3

4.5 (13)

Mar 27, 2026

by @nehawood

Problem
Easy
Free

Reverse Words in a String III

Reverse the characters of every word in a sentence while keeping the word order intact.

strings
two-pointers

765

7

4.3 (11)

Mar 5, 2026

by @meinakamura

Problem
Medium
Free

Subarray Product Less Than K

Count contiguous subarrays whose product is strictly less than k, in linear time using a sliding window.

arrays
sliding-window
two-pointers

817

13

4.3 (14)

Mar 4, 2026

by @yukisantos

Problem
Easy
Free

Intersection of Two Linked Lists

Find the node where two singly-linked lists merge using the two-pointer rendezvous trick that runs in O(m + n) time and O(1) space.

linked-list
two-pointers
hash-table

709

4

Mar 1, 2026

by @folakemansour

Problem
Easy
Free

Two Sum IV - Input is a BST

Decide whether two distinct BST nodes sum to a target, ideally in O(n) time and O(h) extra space.

trees
bst
two-pointers
dfs

418

9

Feb 28, 2026

by @owentoure

Problem
Easy
Free

Palindrome Linked List

Decide whether a singly-linked list reads the same forward and backward in O(n) time and O(1) space using fast/slow pointers plus an in-place reverse of the second half.

linked-list
two-pointers

974

12

4.6 (16)

Feb 21, 2026

by CodeSnatch

Problem
Easy
Free

Move Zeroes

Move every zero to the end of the array in place while keeping the relative order of the non-zero elements.

arrays
two-pointers
fundamentals

505

7

4.2 (10)

Feb 7, 2026

by @rajtanaka

Problem
Easy
Free

Backspace String Compare

Compare two strings after applying backspace edits, with the catch that the O(1) space version uses two pointers walking from the end.

stack
two-pointers
strings

188

5

4.5 (13)

Feb 3, 2026

by @oliviafoster

Problem
Medium
Free

Find All Duplicates in an Array

Identify every value that appears twice in an array of integers in [1, n], using O(1) extra space via index-negation.

arrays
hash-table
two-pointers

927

29

Jan 31, 2026

by @arjunrivera

Problem
Easy
Free

Squares of a Sorted Array

Return the squares of a sorted integer array, also sorted, in O(n) time using a two-pointer merge from the ends.

arrays
two-pointers
sorting

1.1k

22

4.5 (11)

Jan 27, 2026

by @imanichen

Problem
Medium
Free

Find K Closest Elements

Find a length-k window in a sorted array whose elements are closest to x, using a binary search on the LEFT edge of the window.

binary-search
two-pointers
arrays

786

19

4.5 (14)

Jan 20, 2026

by @jameszhang

Problem
Easy
Free

Reverse String

Reverse a character array in place using two pointers, no extra buffer allowed.

strings
two-pointers
fundamentals

354

1

4.4 (9)

Jan 17, 2026

by CodeSnatch

Problem
Easy
Free

Reverse Vowels of a String

Reverse only the vowels of a string while keeping consonants and punctuation in place.

strings
two-pointers

788

5

4.3 (13)

Jan 12, 2026

by @meinakamura

Problem
Hard
$8.99

Wildcard Matching

Match a string against a wildcard pattern with `?` (any single char) and `*` (any sequence) using O(m*n) 2D DP.

dynamic-programming
strings
two-pointers

957

24

Dec 22, 2025

by @rajreeves

Problem
Easy
Free

Sort Array By Parity

Rearrange the array so that every even integer appears before every odd integer; any valid arrangement is accepted.

arrays
two-pointers
fundamentals

180

2

4.0 (9)

Dec 20, 2025

by @riverbanda

Article

Two Pointers Technique

The three sub-patterns (opposite ends, same direction, fast-slow), the canonical problems each one solves, and why recognizing the shape is the entire skill.

two-pointers
array-manipulation-patterns
algorithms
interview-prep
fundamentals

175

5

Dec 10, 2025

by @liamsuzuki