Skip to main content
Two PointersPatterns

Two Pointers: The Complete Guide

· 9 min read

Syed Peera Saheb

Software Engineer · 5+ years in tech interviews

Summary

Converging, same-direction, fast & slow — the two-pointer family is bigger than most engineers realize. Every variant, with examples.

Variant 1: Converging pointers

Left starts at 0, right starts at end. They move toward each other. Works on sorted arrays with a monotonic property. Examples: Two Sum II, Container With Most Water, Trapping Rain Water. The key invariant: whichever pointer is "worse" moves inward.

Variant 2: Same-direction (slow/fast)

Both pointers start at 0. The fast pointer scans ahead; the slow pointer is the "write head". Used for in-place modifications: Remove Duplicates, Move Zeroes, Remove Element. The slow pointer only advances when we want to keep the current fast element.

Variant 3: Fast and slow (Floyd's)

Slow moves 1 step, fast moves 2 steps. Used for cycle detection in linked lists and arrays. If they meet, a cycle exists. To find the cycle entry point: reset one pointer to head, advance both 1 step at a time — they meet at the entry.

Variant 4: Two arrays

One pointer in each of two sorted arrays. Used for merging sorted arrays, finding intersection, and comparing sequences. Merge Two Sorted Lists / Merge k Sorted Lists use this variant. Advance whichever pointer has the smaller current element.

Frequently Asked Questions

What is the two pointers technique?
The two pointers technique uses two index variables that traverse a data structure — usually an array or linked list — simultaneously. The pointers can converge from both ends, move in the same direction at different speeds, or traverse two separate arrays. It eliminates the need for nested loops, reducing time complexity from O(n²) to O(n) for many problems.
When should I use two pointers vs sliding window?
Use two pointers when you need to find a pair/triplet satisfying a condition in a sorted array, or for in-place array modification. Use sliding window when you need a contiguous subarray/substring with a constraint. The two patterns overlap — sliding window is essentially two same-direction pointers with a maintained window state. If the problem involves a "window" of elements, call it sliding window.
What are the most common two pointers LeetCode problems?
Top two pointers problems: Two Sum II (LC 167), 3Sum (LC 15), Container With Most Water (LC 11), Trapping Rain Water (LC 42), Valid Palindrome (LC 125), Merge Sorted Array (LC 88), Remove Duplicates from Sorted Array (LC 26), and Linked List Cycle (LC 141 — fast/slow variant). 3Sum and Trapping Rain Water are especially common at top companies.
How does Floyd's cycle detection algorithm work?
Floyd's algorithm uses a slow pointer (moves 1 step) and a fast pointer (moves 2 steps). If there is a cycle, fast will eventually lap slow and they will meet inside the cycle. To find the cycle entry point: reset one pointer to the head, advance both one step at a time — they meet exactly at the cycle entry. This works due to a mathematical property of modular arithmetic.

Practice this pattern

See all problems and the code template →

Study pattern

Syed Peera Saheb

Software Engineer · 5+ years · ServiceNow

Software engineer with hands-on experience passing technical interviews at top tech companies. Built Coding Prep Guide to share the pattern-first prep strategy that actually works. Writes about DSA, system design, and interview strategy.

Buy me a coffee