-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathSolution.java
38 lines (31 loc) · 851 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// github.com/RodneyShag
// Algorithm
//
// 1. Create a pointer that moves 1 step at a time: 'slow'
// 2. Create a pointer that moves 2 steps at a time: 'fast'
// 3. If the Linked List has a cycle, 'slow' and 'fast' will collide.
/*
A Node is defined as:
class Node {
int data;
Node next;
}
*/
// If "slow" and "fast" collide, we must have a cycle
boolean hasCycle(Node head) {
if (head == null) {
return false;
}
Node slow = head; // moves 1 Node at a time
Node fast = head; // moves 2 Nodes at a time
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
return true; // since "slow" and "fast" collided
}
}
return false;
}
// Time Complexity: O(n)
// Space Complexity: O(1)