time O(n)
space O(1)
3
2
0
-4
Initialize the linked list.
Pseudocode
Pseudocode
1function hasCycle(head):
2 if head == null: return false
3 slow = head
4 fast = head
5 while fast != null and fast.next != null:
6 slow = slow.next
7 fast = fast.next.next
8 if slow == fast:
9 return true
10 return false