intersectionDetectionHashSet<T> function

LinkedListNode<T>? intersectionDetectionHashSet<T>(
  1. LinkedListNode<T>? headA,
  2. LinkedListNode<T>? headB
)

Implementation

LinkedListNode<T>? intersectionDetectionHashSet<T>(
  LinkedListNode<T>? headA,
  LinkedListNode<T>? headB,
) {
  final visited = HashSet<LinkedListNode<T>>();
  var currA = headA;
  while (currA != null) {
    visited.add(currA);
    currA = currA.next;
  }
  var currB = headB;
  while (currB != null) {
    if (visited.contains(currB)) return currB;
    currB = currB.next;
  }
  return null;
}