All files / leetCode 0142.ts

100% Statements 9/9
100% Branches 6/6
100% Functions 1/1
100% Lines 8/8

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          5x 2x   3x   3x 13x 2x 11x     1x    
import type { ListNode } from './List'
 
export default function detectCycle(
  head: ListNode<number> | null,
): ListNode<number> | null {
  if (head === null || head.next === null)
    return null
 
  const set = new Set<ListNode<number>>()
 
  for (let node = head; node !== null; node = node.next as ListNode<number>) {
    if (set.has(node))
      return node
    set.add(node)
  }
 
  return null
}