Skip to content

Commit 78bec4d

Browse files
committed
Linked List Cycle
1 parent bfe247c commit 78bec4d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

linked-list-cycle/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
public class Solution {
4+
public boolean hasCycle(ListNode head) {
5+
ListNode slow = head;
6+
ListNode fast = head;
7+
8+
while (fast != null && fast.next != null) {
9+
slow = slow.next;
10+
fast = fast.next.next;
11+
if (slow == fast) return true;
12+
}
13+
return false;
14+
}
15+
}

0 commit comments

Comments
 (0)