Skip to content

Commit 5e2292a

Browse files
committed
linked list cycle solved
1 parent 4dfafee commit 5e2292a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

linked-list-cycle/mintheon.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) {
7+
* val = x;
8+
* next = null;
9+
* }
10+
* }
11+
*/
12+
//시간복잡도: O(n)
13+
//공간복잡도: O(1)
14+
public class Solution {
15+
public boolean hasCycle(ListNode head) {
16+
ListNode slow = head;
17+
ListNode fast = head;
18+
19+
while(fast != null && fast.next != null) {
20+
slow = slow.next;
21+
fast = fast.next.next;
22+
23+
if(slow == fast) return true;
24+
}
25+
26+
return false;
27+
}
28+
}

0 commit comments

Comments
 (0)