By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This pattern appears in 1 out of every 3 onsite interviews—nail it and you show clear problem-solving maturity, not just brute-force thinking."
Before tackling cycle detection, ensure you understand: 1. Linked List Basics – Node structure, traversal, and pointer manipulation. 2. Two-Pointer Techniques – Slow/fast pointers for linear time solutions. 3. Cycle Detection Intuition – Why a cycle means a pointer revisits a node.
Run this every time you see a cycle detection problem:
Ask: "Is an empty list or single-node list considered a cycle?" (Usually no.)
Choose the Right Technique
Hash Set → O(n) space, O(n) time (fallback if Floyd’s is unclear).
Initialize Two Pointers
slow = head
fast = head
Edge case: If head is null, return false.
head
null
false
Traverse the List
slow
fast
If slow == fast → cycle exists.
slow == fast
Handle Follow-Ups (If Asked)
Cycle Start: After meeting, reset one pointer to head, move both at 1 step until they meet.
Test Edge Cases
Large list with cycle at the end.
Code & Optimize
fast.next
Problem: Given head of a linked list, return true if there’s a cycle.
true
def hasCycle(head): if not head: return False slow = head fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return True return False
Problem: Return the node where the cycle begins.
def detectCycle(head): if not head: return None slow = head fast = head # Find meeting point while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: break # No cycle if not fast or not fast.next: return None # Find cycle start slow = head while slow != fast: slow = slow.next fast = fast.next return slow
Problem: Return the length of the cycle.
def cycleLength(head): if not head: return 0 slow = head fast = head # Find meeting point while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: break if not fast or not fast.next: return 0 # Calculate cycle length length = 1 fast = fast.next while slow != fast: fast = fast.next length += 1 return length
NullPointerException
while fast and fast.next
fast = fast.next.next
if not head
"Alright, let’s lock this in. Cycle detection in linked lists? You’ve got two options: Floyd’s Tortoise and Hare or a hash set. Floyd’s is the gold standard—O(1) space, O(n) time. Here’s the playbook:
This isn’t just about memorizing code. It’s about proving you can derive the solution under pressure. Walk in, write clean code, and explain the math. You’ve got this."
Now go crush that interview. ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.