Conversation
kyra-patton
left a comment
There was a problem hiding this comment.
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.
| if not headA or not headB: | ||
| return None | ||
| p1, p2 = headA, headB | ||
| while p1.val!= p2.val: |
There was a problem hiding this comment.
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: |
| p1 = p1.next if p1.next else headB | ||
| p2 = p2.next if p2.next else headA |
There was a problem hiding this comment.
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.