Skip to content

Commit 090ccc5

Browse files
committed
liked-list-cycle
1 parent e682e7b commit 090ccc5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

linked-list-cycle/jdy8739.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {ListNode} head
11+
* @return {boolean}
12+
*/
13+
var hasCycle = function (head) {
14+
if (!head?.next) {
15+
return false;
16+
}
17+
18+
let current = head;
19+
20+
const set = new Set();
21+
22+
while (current.next) {
23+
if (set.has(current)) {
24+
return true;
25+
}
26+
27+
set.add(current);
28+
current = current.next;
29+
}
30+
31+
return false;
32+
};
33+
34+
// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로
35+
// 공간복잡도 O(n) -> set에 최대 리스트의 노드 수 만큼 size가 증가되므로

0 commit comments

Comments
 (0)