Starting Point of Loop
Find the node where a cycle begins in a linked list
Code
1def detectCycle(head):2 if not head or not head.next:3 return None45 # Phase 1: Detect if cycle exists6 slow = fast = head7 while fast and fast.next:8 slow = slow.next9 fast = fast.next.next10 if slow == fast:11 break12 else:13 return None # No cycle1415 # Phase 2: Find starting point16 ptr1 = head17 ptr2 = slow18 while ptr1 != ptr2:19 ptr1 = ptr1.next20 ptr2 = ptr2.next2122 return ptr1
Function definition
Linked List
INIT
Step
0 / 10
Phase
Initialize
Nodes
6
Progress
Processing
Searching for the starting point of the cycle...
Pointer State
Pointers
slow
1
fast
1
ptr1
null
ptr2
null
Key Insight
After slow/fast meet, moving ptr1 to head and advancing both by 1 until they meet finds the cycle start mathematically.
Explanation
Setup
Initialize Pointers
Set both slow and fast pointers to the head of the list.
💡 Tip
Phase 1: Detect if a cycle exists.