Two Pointers Summary Resource - aloalgo

Two Pointers Summary

A quick reference for two pointer techniques and problems to practice.

Pattern Overview

PatternWhen to UseTime
Opposite EndsSorted array + find pair, palindrome checkO(n)
Same DirectionIn-place modifications, merging arraysO(n)
Fast & SlowCycle detection, find middle of listO(n)

Chapters

  • Basics - Core concepts, when to use each pattern
  • Opposite Ends - Two Sum, 3Sum, Container With Water
  • Same Direction - Remove duplicates, move elements, merge arrays
  • Fast & Slow - Cycle detection, find middle, Floyd's algorithm

Quick Decision Guide

If you see...Use...
Sorted array + find pair with sumOpposite ends
Check if palindromeOpposite ends
Remove/modify elements in-placeSame direction
Merge two sorted arraysSame direction
Linked list cycleFast & slow
Find middle of linked listFast & slow
Reduce O(n²) to O(n)Usually opposite ends

Practice Problems

Opposite Ends

Fast & Slow

Common Mistakes

  1. Off-by-one errors: Be careful with loop conditions (left < right vs left <= right).
  2. Forgetting to check fast.next: In fast/slow, always verify fast and fast.next exist before moving.
  3. Wrong pointer to move: In opposite ends, think carefully about which pointer movement can improve the result.
  4. Skipping duplicates incorrectly: In 3Sum-type problems, skip duplicates after finding a valid solution, not before.

Key Takeaways

  • Two pointers is about eliminating redundant comparisons by using the structure of the problem.
  • All three patterns achieve O(n) time and O(1) space.
  • The key decision is always which pointer to move and why.
  • Two pointers often appears as a subroutine in larger problems (e.g., 3Sum uses 2Sum).
Was this helpful?
© 2026 aloalgo. All rights reserved.