Skip to content

Commit 1ea80be

Browse files
committed
solve: 141. Linked List Cycle
Runtime: 53 ms, faster than 94.85% of JavaScript online submissions for Linked List Cycle. Memory Usage: 52.7 MB, less than 80.49% of JavaScript online submissions for Linked List Cycle.
1 parent 40ee4bd commit 1ea80be

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

linked-list-cycle/evan.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
let fastPointer = head;
15+
let slowPointer = head;
16+
17+
while (fastPointer !== null && slowPointer !== null) {
18+
fastPointer = fastPointer?.next?.next;
19+
slowPointer = slowPointer?.next;
20+
21+
if (fastPointer === slowPointer) {
22+
return true;
23+
}
24+
}
25+
26+
return false;
27+
};

0 commit comments

Comments
 (0)