I was trying to solve a problem: find middle node of a linked list with following code ( and it works)
s = f = head
while f and f.next:
s = s.next
f = f.next.next
return s
but when I accidentally wrote this it failed:
s = f = head
while f.next and f:
s = s.next
f = f.next.next
return s
with error
AttributeError: 'NoneType' object has no attribute 'next'
while f.next and f:
I am trying to find out why this behavior, both loops check if f and f.next are not None. why order matters here?