Linked List Cycle II

MediumHash TableLinked ListTwo Pointers

Solution

import { ListNode } from '@algorithm/lib';
 
export function detectCycle(head: ListNode | null): ListNode | null {
  let slow = head;
  let fast = head;
  while (slow && slow.next && fast && fast.next) {
    slow = slow.next;
    fast = fast.next.next;
    if (slow === fast) break;
  }
  if (!(fast && fast.next)) {
    return null;
  }
  let entry = head;
  while (entry && slow && entry !== slow) {
    entry = entry.next;
    slow = slow.next;
  }
  return entry;
}