From 0f34b3c4c78d56cff8c17b9df534359c8fbd536e Mon Sep 17 00:00:00 2001 From: Tirhas Gebreyohannes Date: Sat, 8 Oct 2022 20:08:07 -0700 Subject: [PATCH] All tests passed --- linked_lists/intersection.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f07e2ae..0e64588 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -6,9 +6,28 @@ def __init__(self, value): self.val = value self.next = None - +# time complexity O(n) +# space complexity O(n) def intersection_node(headA, headB): """ Will return the node at which the two lists intersect. If the two linked lists have no intersection at all, return None. """ - pass \ No newline at end of file + if headA is None or headB is None: + return None + elif headA is None and headB is None: + return None + else: + current = headA + nodes_set = set() + while current: + if current not in nodes_set: + nodes_set.add(current) + current = current.next + + current_b = headB + while current_b: + if current_b in nodes_set: + return current_b + current_b = current_b.next + return None + \ No newline at end of file