Skip to content

Commit c7db4da

Browse files
authored
solution on linked-list-cycle
1 parent 9f67988 commit c7db4da

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

linked-list-cycle/Lustellz.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
// Runtime: 49ms
14+
// Memory: 56.94MB
15+
16+
function hasCycle(head: ListNode | null): boolean {
17+
let fast: ListNode = head;
18+
let slow: ListNode = head;
19+
20+
while (fast && fast.next) {
21+
fast = fast.next.next;
22+
slow = slow!.next;
23+
24+
if (fast === slow) {
25+
return true;
26+
}
27+
}
28+
return false;
29+
}

0 commit comments

Comments
 (0)