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.
2 parents 38d28d2 + d2a3e67 commit 23d4979Copy full SHA for 23d4979
1 file changed
linked-list-cycle/robinyoon-dev.js
@@ -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
35
36
37
38
+ return result;
39
40
+ function findElementInDp(head) {
41
+ if (visited.has(head)) {
42
+ return true;
43
44
+ return false;
45
46
47
+};
0 commit comments