Skip to content

Commit 23d4979

Browse files
authored
Merge pull request #2574 from robinyoon-dev/main
[robinyoon-dev] Week 09 Solutions
2 parents 38d28d2 + d2a3e67 commit 23d4979

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

linked-list-cycle/robinyoon-dev.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
15+
const visited = new Set();
16+
let result = false;
17+
18+
while (head) {
19+
20+
let next = head.next;
21+
22+
if (next == null) {
23+
result = false;
24+
break;
25+
}
26+
27+
let isExist = findElementInDp(head);
28+
29+
if (!isExist) {
30+
visited.add(head);
31+
head = next;
32+
} else {
33+
result = true;
34+
break;
35+
}
36+
}
37+
38+
return result;
39+
40+
function findElementInDp(head) {
41+
if (visited.has(head)) {
42+
return true;
43+
} else {
44+
return false;
45+
}
46+
}
47+
};

0 commit comments

Comments
 (0)