Skip to content

Two pointers [mp]

Micro pattern

Description

Coordinate moving positions so each move eliminates work or preserves a structural invariant.

Problems

Opposite ends

Move inward from both ends using comparisons or boundary guarantees.

Read and write pointers

Use one position for input and another for the next retained output.

Backward merge pointers

Read two sorted suffixes while writing the larger value into a destination suffix.

Sliding-window boundaries

Advance the right boundary and move the left boundary to maintain validity.

Slow and fast pointers

Advance at different speeds or maintain a fixed gap.

Equalize path lengths by switching heads

Make each pointer traverse both lists to equalize total path lengths.

Multiple linked-list chains

Maintain separate tails for independently rearranged portions.

Three-way partition pointers

Maintain boundaries for lower, active, and upper categories.

Matrix staircase pointer

Move across an ordered matrix while discarding rows or columns.

Text and pattern pointers

Advance text and pattern positions with a saved fallback point.