Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C17/CSFunA/Huma Hameed #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion linked_lists/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,43 @@ def __init__(self, value):
self.val = value
self.next = None

def len_check(head):
head_len = 0

while head:
head_len += 1
head = head.next

return head_len


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
if not headA or not headB:
return None

headA_len = len_check(headA)
headB_len = len_check(headB)

if headA_len > headB_len:
move = headA_len - headB_len
while move:
headA = headA.next
move -= 1
elif headB_len > headA_len:
move = headB_len - headA_len
while move:
headB = headB.next
move -= 1

while headA:
if headA != headB:
headA = headA.next
headB = headB.next
elif headA == headB:
node_intersection = headA
return node_intersection

return None