Skip to content

Commit 3481e87

Browse files
committed
feat: 141. Linked List Cycle
1 parent d6a09dc commit 3481e87

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

linked-list-cycle/gwbaik9717.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)