Skip to main content

Two Pointers Algorithm

🤖WARNING
The English translation was done by AI.

Common Types of Two Pointers Algorithm

  • Two Pointers with Opposite Direction
  • Two Pointers with the Same Direction
  • Two Pointers with Opposite Direction

Two Pointers with Opposite Direction

Validating Palindrome String

leetcode

Validating Palindrome String II

There are two cases of deleting characters: deleting from the left side and deleting from the right side

leetcode

leetcode

Two Sum

Sort the array first and then use two pointers

leetcode

Two Pointers with the Same Direction

Removing Nth Node from the End of a Linked List

Use the distance of N between two pointers to achieve the fast pointer reaching the end, and the slow pointer will be the Nth node from the end leetcode

Detecting a Cycle in a Linked List

Use fast and slow pointers with a while loop. If the fast pointer meets the slow pointer, it means there is a cycle

leetcode

Reversing a Linked List

Use two pointers to keep track of the current node and the previous node. After keeping track, break the next link

leetcode

Fast and Slow Pointers

Commonly seen problems: modifying arrays in-place

Remove Duplicates from Sorted Array

Remove Duplicates from Sorted List

Sliding Window

https://labuladong.github.io/algo/di-yi-zhan-da78c/shou-ba-sh-48c1d/wo-xie-le--f7a92/