All files / leetCode 0141.ts

100% Statements 8/8
100% Branches 8/8
100% Functions 1/1
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20      5x 2x     3x 3x         6x 1x     2x    
import type { ListNode } from './List'
 
export default function hasCycle(head: ListNode<number> | null): boolean {
  if (head === null || head.next === null)
    return false
 
  // 龟兔赛跑
  for (
    let slow = head, fast = head.next;
    slow !== fast;
    slow = slow.next as ListNode<number>,
    fast = fast.next.next as ListNode<number>
  ) {
    if (fast === null || fast.next === null)
      return false
  }
 
  return true
}