Skip to main content

Linked List

Cycle Detection

Floyd's Tortoise and Hare

龜兔賽跑算法,slow和fast都從head開始

  • Tortoise (slow pointer)一次前進1
  • Hare (fast pointer)一次前進2

如果Linked List內存在Cycle,slow跟fast必定會相遇