From 8c655932c40f9ecd24789a544a0b945a213ceb58 Mon Sep 17 00:00:00 2001 From: camilla Date: Mon, 2 Jan 2023 19:12:36 -0500 Subject: [PATCH] Two solutions --- linked_lists/intersection.py | 44 ++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f07e2ae..2ca8bb6 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -1,14 +1,44 @@ - - - class Node: def __init__(self, value): self.val = value self.next = None +# BRUTE FORCE SOLUTION +# 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. +# """ +# currentA = headA + +# while currentA: +# currentB = headB + +# while currentB: +# if currentA == currentB: +# return currentA +# currentB = currentB.next + +# currentA = currentA.next + +# return None + +# SECOND SOLUTION 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 + d = {} + + currentA = headA + + while currentA: + d[currentA] = 0 + currentA = currentA.next + + currentB = headB + + while currentB: + if currentB in d: + return currentB + + currentB = currentB.next + + return None \ No newline at end of file