Reputation: 3
when working with online problems I ran into an issue in this section of code:
while list1 and list2:
if list1.val < list2.val:
tracker.next = list1
list1, tracker = list1.next, list1
This works just fine, but when I simply rearrange it for readability into:
while list1 and list2:
if list1.val < list2.val:
cur.next = list1
list1 = list1.next
tracker = list1
I run into a timeout error and I cannot see how such a simple change can do this.
Am I missing something? or is the change in time drastic enough to affect the program?
Upvotes: 0
Views: 76
Reputation: 12140
This is not a simple rearrangement. The second example has a different logic, i.e. in the first snippet list1
is changed to the value of list1.next
, and tracker
is changed to the value of list1
(before it was changed to list1.next
). While in the second example tracker
is changed to the value of list1
after list1
is changed to list1.next
. In other words list1
and tracker
are always the same.
Upvotes: 1