mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 04:36:08 -04:00
Create finding_intersection.py
This commit is contained in:
parent
654bb8c76f
commit
eb36c2249d
26
linked_lists/finding_intersection.py
Normal file
26
linked_lists/finding_intersection.py
Normal 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
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user