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