Intersection of Two Linked Lists

EasyHash TableLinked ListTwo Pointers

Solution

import { ListNode } from '@algorithm/lib';
 
export function getIntersectionNode(
  headA: ListNode | null,
  headB: ListNode | null,
): ListNode | null {
  let [nodeA, nodeB] = [headA, headB];
  while (nodeA !== nodeB) {
    nodeA = nodeA ? nodeA.next : headB;
    nodeB = nodeB ? nodeB.next : headA;
  }
  return nodeA;
}