master-algorithms-py/linked_lists/finding_intersection.py
2023-08-07 15:47:08 -07:00

27 lines
536 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Node:
def __init__(self, val):
self.val = val
self.next = None
def get_intersection_node(self, head_a: Node, head_b: Node) -> Optional[Node]:
seen_b = set()
while head_b is not None:
seen_b.add(head_b)
head_b = head_b.next
while head_a is not None:
if head_a in seen_b:
return head_a
head_a = head_a.next