Linked List Runner Technique by Prateek Sir in Algo++ Course

the code given by sir–
node* midPoint(nodehead){
if(head==NULL||head->next==NULL){
return head;
}
node
slow=head;
node*fast=head->next; //MyDoubt
while(fast!=NULL && fast->next!=NULL){
fast=fast->next->next;
slow=slow->next;
}
return slow;
}

My doubt is in the 4th and the 5th line where slow pointer and fast pointer is initialized.So according to those 2 lines slow will point to the first node and fast will point to the second node before the loop begins.this means that the starting point of the slow and fast pointer is not same but i think that it should be same.According to me before the start of the loop( or say race) the starting point of both the pointers should be same.Please clear my doubt…

Hey Pratyush, Yes before the start of the loop the starting point of both the pointers should be same according to the theorem but in this function as we are just calculating the mid point node* slow=head; node* fast=head->next; will also give the right answer.

1 Like