Linked List Cycle

EasyHash TableLinked ListTwo Pointers

Solution

import { ListNode } from '@algorithm/lib';
 
export function hasCycle(head: ListNode | null): boolean {
  if (head === null) {
    return false;
  }
 
  let slow: ListNode | null = head;
  let fast: ListNode | null = head.next;
 
  while (slow && fast) {
    if (slow === fast) {
      return true;
    }
    slow = slow.next;
    fast = fast.next ? fast.next.next : null;
  }
  return false;
}