I am learning the following code for Middle of Linked List:
class Solution(object):
def middleNode(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
I noticed that if I use "while fast.next" instead of "while fast and fast.next" for the loop, I got the same results. May I ask why I should use both fast and fast.next to stop the while loop? From my understanding, if fast.next is None, fast is always none, is this correct?
Thank you!