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

Tirhas #84

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
23 changes: 21 additions & 2 deletions linked_lists/intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@ def __init__(self, value):
self.val = value
self.next = None


# time complexity O(n)
# space complexity O(n)
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 headA is None or headB is None:
return None
elif headA is None and headB is None:
return None
else:
current = headA
nodes_set = set()
while current:
if current not in nodes_set:
nodes_set.add(current)
current = current.next

current_b = headB
while current_b:
if current_b in nodes_set:
return current_b
current_b = current_b.next
return None