From fab46acc65bdd082fe3164c64a2d1cda24a79f89 Mon Sep 17 00:00:00 2001 From: Ivana Maldonado Date: Wed, 18 Jan 2023 00:57:51 -0500 Subject: [PATCH] Finished linked lists exercise. --- linked_lists/intersection.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f07e2ae..952b2f4 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -11,4 +11,24 @@ 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 + + #linked_list_a: 3 -> 4 -> 5 -> 6 -> 7 + # linked_list_b: 1 -> 2 -> 5 -> 6 -> 7 + + if not headA or not headB: + return None + + current_a = headA + current_b = headB + + while current_b: + while current_a: + if current_a == current_b: + return current_a + current_a = current_a.next + + current_a = headA + current_b = current_b.next + + return None + \ No newline at end of file