Skip to content

Commit 97f8fdc

Browse files
committed
linked list cycle solution
1 parent f2b9477 commit 97f8fdc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/limlimjo.js

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

0 commit comments

Comments
 (0)