We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e682e7b commit 090ccc5Copy full SHA for 090ccc5
linked-list-cycle/jdy8739.js
@@ -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
32
+};
33
34
+// 시간복잡도 O(n) -> 최대 리스트의 노드 수 만큼 while문이 반복되므로
35
+// 공간복잡도 O(n) -> set에 최대 리스트의 노드 수 만큼 size가 증가되므로
0 commit comments