***************ll mid point

wrong output
#include
using namespace std;
class node{
public:
int data;
node *next;
node(int d){
data = d;
next = NULL;
}
};

void insertAtTail(node *&head, int data){
if(head==NULL){
head= new node(data);
return;
}
node *tail = head;
while(tail->next!=NULL){
tail=tail->next;
}
tail->next=new node(data);
return;
}
void buildInput(node *&head){
int data;
cin>>data;
while(data!=-1){
insertAtTail(head,data);
cin>>data;
}
}

//racer technique
node *midPoint(node *head){
if(head==NULL || head->next==NULL){
return head;
}
node *slow =head;
node *fast = head->next;
while (fast!=NULL && fast->next==NULL){

    fast=fast->next->next;
     slow=slow->next;
}
return slow;

}

void print(node *head){
//node *temp=head;
while(head!=NULL){
cout<data;
head=head->next;
}
}
int main(){
node *head=NULL;
buildInput(head);
node *m= midPoint(head);
cout<data;
//print(head);
return 0;}

@ayu2321, can you send me the code using https://ide.codingblocks.com/ due to formatting issue its not completely rendered

@ayu2321, there are two errors in your code :-
in your code

  1. at line 40, it should be node *fast = head; instead of node *fast = head->next; since both pointers should start from the same point.
  2. at line 41, your condition in while is wrong instead of while (fast!=NULL && fast->next==NULL){ it should be while (fast!=NULL && fast->next!=NULL){

corrected code :-

node*midPoint(node *head){
if(head==NULL || head->next==NULL){
    return head;
}
node *slow =head;
node *fast = head;
while (fast!=NULL && fast->next!=NULL){
    fast=fast->next->next;
    slow=slow->next;
}
return slow;
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.