-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (27 loc) · 923 Bytes
/
Copy pathSolution.java
File metadata and controls
29 lines (27 loc) · 923 Bytes
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
public class Solution {
//Time Complexity: O(n)
//Space Complexity: O(1)
public boolean hasCycle(ListNode head) {
ListNode fast = head, slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (slow == fast) return true;
}
return false;
}
public static void main(String[] args) {
int[] nums = {3, 2, 0, -4};
int pos = 1;
ListNode head = new ListNode(nums, pos);
System.out.println(new Solution().hasCycle(head));//true
int[] nums1 = {1, 2};
int pos1 = 0;
ListNode head1 = new ListNode(nums1, pos1);
System.out.println(new Solution().hasCycle(head1));//true
int[] nums2 = {1};
int pos2 = -1;
ListNode head2 = new ListNode(nums2, pos2);
System.out.println(new Solution().hasCycle(head2));
}
}