From aba0e5d009a1cfbef26733caeb67155cc71f64a3 Mon Sep 17 00:00:00 2001 From: Hope Olaide Date: Thu, 19 Jan 2023 16:48:02 -0500 Subject: [PATCH 1/2] ADA Linked Lists Assignment Complete --- linked_lists/intersection.py | 38 +++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index f07e2ae..840f9ca 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -6,9 +6,41 @@ def __init__(self, value): self.val = value self.next = None - -def intersection_node(headA, headB): +# 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 +def find_intersection(headA, headB): + len_lla = 0 + lla_current = headA + while lla_current: + len_lla += 1 + lla_current = lla_current.next + + len_llb = 0 + llb_current = headB + while llb_current: + len_llb += 1 + llb_current = llb_current.next + +#traverse the list by prioritizing the longer of the two list options + lla_current = headA + llb_current = headB + + if len_lla > len_llb: + for i in range(len_lla - len_llb): + lla_current = lla_current.next + else: + for i in range(len_llb - len_lla): + llb_current = llb_current.next + + while lla_current and llb_current: + if lla_current == llb_current: + return lla_current + + lla_current == lla_current.next + llb_current == llb_current.next + + return None + + From bcd0d61e32a580421f7d45a55c3aee732c418148 Mon Sep 17 00:00:00 2001 From: Hope Olaide Date: Thu, 19 Jan 2023 17:17:33 -0500 Subject: [PATCH 2/2] ADA Intersection of Linked Lists assignment complete. --- linked_lists/intersection.py | 52 ++++++++++------------------------- linked_lists/requirements.txt | 0 2 files changed, 14 insertions(+), 38 deletions(-) create mode 100644 linked_lists/requirements.txt diff --git a/linked_lists/intersection.py b/linked_lists/intersection.py index 840f9ca..996ee59 100644 --- a/linked_lists/intersection.py +++ b/linked_lists/intersection.py @@ -1,46 +1,22 @@ - - class Node: - def __init__(self, value): - self.val = value - self.next = None + def __init__(self, value, next_node=None): + self.value = value + self.next = next_node -# def intersection_node(headA, headB): + +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. """ -def find_intersection(headA, headB): - len_lla = 0 - lla_current = headA - while lla_current: - len_lla += 1 - lla_current = lla_current.next - - len_llb = 0 - llb_current = headB - while llb_current: - len_llb += 1 - llb_current = llb_current.next - -#traverse the list by prioritizing the longer of the two list options - lla_current = headA - llb_current = headB - - if len_lla > len_llb: - for i in range(len_lla - len_llb): - lla_current = lla_current.next - else: - for i in range(len_llb - len_lla): - llb_current = llb_current.next - - while lla_current and llb_current: - if lla_current == llb_current: - return lla_current - - lla_current == lla_current.next - llb_current == llb_current.next - - return None + if headA is None or headB is None: + return None +# Use current values for the pointers: + pointer_a, pointer_b = headA, headB +# Use a while loop that + while pointer_a != pointer_b: + pointer_a = headB if pointer_a is None else pointer_a.next + pointer_b = headA if pointer_b is None else pointer_b.next + return pointer_a diff --git a/linked_lists/requirements.txt b/linked_lists/requirements.txt new file mode 100644 index 0000000..e69de29