Create finding_intersection.py

This commit is contained in:
marina 2023-08-02 17:14:46 -07:00 committed by GitHub
parent 654bb8c76f
commit eb36c2249d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,26 @@
#!/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[ListNode]:
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