Two pointers is a competitive programming approach. In this method, two pointers are used to iterate through an array. Each pointer can only move to one direction (for example, one to the left from one end, the other to the right from the other).

Fast and slow pointers

One variation on two pointers is to have one pointer iterate slowly and the other iterate faster, called Floyd’s algorithm. This is especially helpful for working with linear data structures.

This is a powerful idea in general, because it basically ensures we can solve the question in a single pass instead of multiple. Having one pointer be ahead and another be behind — then moving each one (either by the same amount or with one moving by more).

For linked list questions:

  • For cyclic questions, we use this because it’s guaranteed the fast and slow pointers will meet at some point, and it uses space complexity.
  • To find the halfway element, we also do this because the slow pointer will reach the middle at the same time the fast pointer reaches the end.

redo this one https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/submissions/ one pointer goes ahead by 2, the other goes ahead by 1