While writing a code for finding the the middle of the linked List. I encounter a problem where I am not able to understand why one one the snipper works while other does not. Snippet Working
ListNode *slow = head, *fast = head;
while (fast && fast->next)
slow = slow->next, fast = fast->next->next;
return slow;
Snippet not working
ListNode *slow = head, *fast = head;
while (fast->next && fast)
slow = slow->next, fast = fast->next->next;
return slow;
Now in my understading, since we have a "&&" condition, it'll go inside the loop only when both the conditions are met, yet, the second snippet fails on some test cases. Thanks in advance.