forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminji-go.java
More file actions
27 lines (24 loc) ยท 771 Bytes
/
minji-go.java
File metadata and controls
27 lines (24 loc) ยท 771 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
/**
* <a href="https://leetcode.com/problems/linked-list-cycle/">week9-1. linked-list-cycle</a>
* <li>Description: Return true if there is a cycle in the linked list. </li>
* <li>Topics: Hash Table, Linked List, Two Pointers</li>
* <li>Time Complexity: O(N), Runtime 0ms </li>
* <li>Space Complexity: O(1), Memory 44.37MB</li>
*/
public class Solution {
public boolean hasCycle(ListNode head) {
if (head == null) {
return false;
}
ListNode slow = head;
ListNode fast = head.next;
while (slow != fast) {
if (fast == null || fast.next == null) {
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}