Skip to content

Commit c1930ef

Browse files
committed
feat: linked-list-cycle
1 parent 847ee56 commit c1930ef

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

linked-list-cycle/minji-go.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* <a href="https://leetcode.com/problems/linked-list-cycle/">week9-1. linked-list-cycle</a>
3+
* <li>Description: Return true if there is a cycle in the linked list. </li>
4+
* <li>Topics: Hash Table, Linked List, Two Pointers</li>
5+
* <li>Time Complexity: O(N), Runtime 0ms </li>
6+
* <li>Space Complexity: O(1), Memory 44.37MB</li>
7+
*/
8+
public class Solution {
9+
public boolean hasCycle(ListNode head) {
10+
if (head == null) {
11+
return false;
12+
}
13+
14+
ListNode slow = head;
15+
ListNode fast = head.next;
16+
17+
while (slow != fast) {
18+
if (fast == null || fast.next == null) {
19+
return false;
20+
}
21+
slow = slow.next;
22+
fast = fast.next.next;
23+
}
24+
25+
return true;
26+
}
27+
}

0 commit comments

Comments
 (0)