mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
27 lines
536 B
Python
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
|
|
|