-
Notifications
You must be signed in to change notification settings - Fork 108
all tests passed #98
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
base: main
Are you sure you want to change the base?
all tests passed #98
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice attempt! Looks like there were some slight misunderstandings about the logic. I added some tips on how you might refactor to pass all the tests.
Please reach out if you have questions.
return None | ||
p1, p2 = headA, headB | ||
while p1.val!= p2.val: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to compare the node objects and not just their values. Two nodes can have the same values but be different objects in memory!
while p1.val!= p2.val: | |
while p1 != p2: |
p2 = p2.next if p2.next else headA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your current code causes an infinite loop in some cases.
If you make this change, what will happen is p1
will become each node in list A, then each node in list B and then get set to None
.
p2
will become each node in list B and then each node in list B and get set to None
at the same time as p1
gets set to None
. Then the while loop condition will become Falsey, and you'll return None
.
When you set p1
to p1.next
only if p1.next
exists (and the equivalent with p2
) they never get set to None
therefore you end up in an infinite loop.
Try using the debugger on test case 4 test_will_return_none_when_no_intersection
to see what happens.
p1 = p1.next if p1.next else headB | |
p2 = p2.next if p2.next else headA | |
p1 = p1.next if p1 else headB | |
p2 = p2.next if p2 else headA |
p1 = p1.next if p1.next else headB | |
p2 = p2.next if p2.next else headA | |
p1 = p1.next if p1.next else headB | |
p2 = p2.next if p2.next else headA |
No description provided.