Useslow(1 step) and fast(2 steps) pointers.If they meet → cycle exists.To find cycle start: reset one pointer to head, move both at 1 step.
Example
// JavaScriptfunctionhasCycle(head){let slow = head, fast = head;while(fast && fast.next){
slow = slow.next;
fast = fast.next.next;if(slow === fast)returntrue;}returnfalse;}functionfindCycleStart(head){let slow = head, fast = head;while(fast && fast.next){
slow = slow.next;
fast = fast.next.next;if(slow === fast){
slow = head;while(slow !== fast){
slow = slow.next;
fast = fast.next;}return slow;// cycle start node}}returnnull;}
# Python
def has_cycle(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.nextif slow is fast:returnTruereturnFalse
def find_cycle_start(head):
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.nextif slow is fast:
slow = head
while slow is not fast:
slow = slow.next
fast = fast.nextreturn slow
returnNone
Output
Has cycle: True/False
Cycle start: node where cycle begins
Note Time O(n), Space O(1). The math behind finding the cycle start: when they meet, the distance from head to cycle start equals the distance from meeting point to cycle start (going around). Always use 'is' (identity) not '==' (equality) for node comparison.
When to use:-Cycledetection(linked list, array)-Finding middle of linked list
-Finding the start of a cycle
-Happy number problem
Slow moves 1 step, fast moves 2 steps.
Example
// JavaScript - Happy number (sum of squared digits eventually reaches 1)functionisHappy(n){functiondigitSquareSum(num){let sum =0;while(num >0){const digit = num %10;
sum += digit * digit;
num =Math.floor(num /10);}return sum;}let slow = n, fast = n;do{
slow =digitSquareSum(slow);
fast =digitSquareSum(digitSquareSum(fast));}while(slow !== fast);return slow ===1;}
# Python
def is_happy(n):
def digit_square_sum(num):
total =0while num >0:
digit = num %10
total += digit * digit
num //= 10return total
slow = fast = n
whileTrue:
slow =digit_square_sum(slow)
fast =digit_square_sum(digit_square_sum(fast))if slow == fast:breakreturn slow ==1
Note The fast/slow pointer pattern detects cycles in any sequence where each value maps to the next. It uses O(1) space compared to a hash set. For linked lists, this is Floyd's algorithm. For number sequences (like happy number), the function that generates the next value plays the role of 'next pointer'.
Frequently asked questions
How does Interview Prep handle tortoise hare?
Interview Prep covers this with 2 copy-ready snippets on this page. The "Detect Cycle (Floyd's Tortoise & Hare)" snippet in Interview Prep uses `Use slow (1 step) and fast (2 steps) pointers.`.
Which code does the Interview Prep example use?
The "Detect Cycle (Floyd's Tortoise & Hare)" snippet uses `Use slow (1 step) and fast (2 steps) pointers.`, from the Linked Lists section of the Interview Prep cheat sheet.
What other Interview Prep snippets are shown for "tortoise hare"?
Besides "Detect Cycle (Floyd's Tortoise & Hare)", this page also shows "Pattern: Fast & Slow Pointer".
Is there anything to watch out for?
Yes. For "Detect Cycle (Floyd's Tortoise & Hare)": Time O(n), Space O(1). The math behind finding the cycle start: when they meet, the distance from head to cycle start equals the distance from meeting point to cycle start (going around). Always use 'is' (identity) not '==' (equality) for node comparison.