-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution.ts
52 lines (41 loc) · 965 Bytes
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* @lc app=leetcode id=142 lang=javascript
*
* [142] Linked List Cycle II
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
interface ListNode {
val: number;
next: ListNode | null;
}
/**
* @param {ListNode} head
* @return {ListNode}
*/
const detectCycle = (head: ListNode | null): ListNode | null => {
// * ['60 ms', '95.65 %', '36.5 MB', '56.25 %']
// * https://leetcode-cn.com/problems/linked-list-cycle-ii/solution/huan-xing-lian-biao-ii-by-leetcode/113097/
if (head === null) return null;
let slow = head;
let fast = head;
do {
if (fast.next === null || fast.next.next === null) return null;
slow = slow!.next!;
fast = fast!.next!.next;
} while (slow !== fast);
slow = head;
while (slow !== fast) {
slow = slow!.next!;
fast = fast!.next!;
}
return fast;
};
// @lc code=end
export { detectCycle };