Starting Point of Loop

Find the node where a cycle begins in a linked list

Read Here
Step0 / 10
PhaseInitialize
Nodes6
Start
Back To Linked List

Code

1def detectCycle(head):
2 if not head or not head.next:
3 return None
4
5 # Phase 1: Detect if cycle exists
6 slow = fast = head
7 while fast and fast.next:
8 slow = slow.next
9 fast = fast.next.next
10 if slow == fast:
11 break
12 else:
13 return None # No cycle
14
15 # Phase 2: Find starting point
16 ptr1 = head
17 ptr2 = slow
18 while ptr1 != ptr2:
19 ptr1 = ptr1.next
20 ptr2 = ptr2.next
21
22 return ptr1
Function definition

Linked List

INIT

Step

0 / 10

Phase

Initialize

Nodes

6

null1next2next3next4next5next6nextcurrnextslowfast

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.