Algobase
Problems
Get Premium
Pricing
Problems
/
141. Linked List Cycle
Prev
Next
Visualizer
Problem
Solution
Code
Single Cycle
No Cycle
Small Cycle
Empty
⌥
Visualizer will appear here
Problem
Solution
Code
1
def has_cycle(head):
2
slow = head
3
fast = head
4
while fast and fast.next:
5
slow = slow.next
6
fast = fast.next.next
7
if slow == fast: return True
8
return False
Visualizer
Single Cycle
No Cycle
Small Cycle
Empty
⌥
Visualizer will appear here