141. Linked List Cycle Easy
Python Code
1 class Solution:
2 def hasCycle(self, head):
3 if not head or not head.next:
4 return False
5
6 slow = head
7 fast = head
8
9 while fast and fast.next:
10 slow = slow.next
11 fast = fast.next.next
12
13 if slow == fast:
14 return True
15
16 return False
Visualization
Slow Pointer
-
Fast Pointer
-
Cycle Position
-
Initialize slow and fast pointers at head
0 / 0
Step Log