Boundary comparisons, sorted pair searches, merging, read/write updates, and fast/slow linked-list traversal.
- sorted input with a safe elimination rule
- preserve order in place
- compare from two positions
- merge two sequences
- find a linked-list middle or cycle
Define what each pointer means and which positions are settled or candidates can safely be discarded; the exact promise depends on the problem.
Often O(n) or O(n + m) time. Typical auxiliary space: Often O(1), excluding output.
JAVA STARTING POINT
A template to adapt, not memorize.
int left = 0;
int right = values.length - 1;
while (left < right) {
// Inspect both boundaries and move at least one pointer.
}Common mistakes
- Moving without a safe elimination rule
- Using sorted pair-sum moves on unsorted input
- Losing stable order
- Not advancing on equal values
PUBLISHED / 08
Learn in this order.
Available articles always appear before the publishing plan.
Two Pointers in Java: A Beginner's Guide
Learn two pointers through palindrome checks, sorted pair sums, duplicate removal, merging, and fast/slow linked-list examples, with diagrams and explained Java code.
Read article →Remove Duplicates from a Sorted Array
Use read and write pointers to keep one copy of each value in place.
Read article →Move Zeroes to the End
Move every zero to the end while preserving the order of nonzero values.
Read article →Strobogrammatic Number II in Java: Fill Matching Ends
Generate upside-down-symmetric numbers by filling two ends together, handling leading zero and odd centers, with code and a visual dry run.
Read article →Union of Two Sorted Arrays
Merge two sorted arrays into a sorted list of distinct values with two pointers.
Read article →Output Contest Matches in Java: Pair the Ends, Recurse by Round
Construct ranked tournament brackets round by round, understand Java reference passing, and count the real String-copying cost.
Read article →Strobogrammatic Number III in Java: Count Numbers Between String Bounds
Count upside-down-symmetric numbers in an inclusive string range with paired-digit recursion, safe comparisons, commented Java and a dry run.
Read article →Palindrome Partitioning in Java: Choose a Valid Prefix, Partition the Rest
Enumerate palindrome partitions by choosing valid prefixes, copying completed paths and undoing; understand substring costs and minimum-cut differences.
Read article →PLANNED PRACTICE
What comes after the published lessons.
- Two sum in a sorted arrayPlanned, not yet published
- Container with most waterPlanned, not yet published
