From eb36c2249db66f8483a7f770a4dc9810a9c6cd5a Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Wed, 2 Aug 2023 17:14:46 -0700 Subject: [PATCH] Create finding_intersection.py --- linked_lists/finding_intersection.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 linked_lists/finding_intersection.py diff --git a/linked_lists/finding_intersection.py b/linked_lists/finding_intersection.py new file mode 100644 index 0000000..0b6ccb7 --- /dev/null +++ b/linked_lists/finding_intersection.py @@ -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 +