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 d6a09dc commit 3481e87Copy full SHA for 3481e87
linked-list-cycle/gwbaik9717.js
@@ -0,0 +1,22 @@
1
+// Time complexity: O(n)
2
+// Space complexity: O(1)
3
+
4
+/**
5
+ * @param {ListNode} head
6
+ * @return {boolean}
7
+ */
8
+var hasCycle = function (head) {
9
+ let turtle = head;
10
+ let rabbit = head;
11
12
+ while (turtle && rabbit && rabbit.next) {
13
+ turtle = turtle.next;
14
+ rabbit = rabbit.next.next;
15
16
+ if (turtle === rabbit) {
17
+ return true;
18
+ }
19
20
21
+ return false;
22
+};
0 commit comments