#include
using namespace std;
class node{
public:
int data;
nodenext;
node(int d)
{
data=d;
next=NULL;
}
};
/int length(nodehead)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len+=1;
}
return len;
}/
void reverse(node*&head)
{
nodec=head;
nodep=NULL;
noden;
while(c!=NULL)
{
n=c->next;
c->next=p;
p=c;
c=n;
}
head=p;
}
nodelastelement(nodehead,int pos)
{
if(head==NULL || head->next==NULL)
{
return head;
}
nodefast=head;
int jump=1;
while(jump<pos)
{
fast=fast->next;
jump++;
}
/while(fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}/
return fast;
}
void insertattail(node*&head,int data)
{
if(head==NULL)
{
head=new node(data);
return;
}
nodetail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(data);
return;
}
void print(nodehead)
{
while(head!=NULL)
{
cout<data;
head=head->next;
}
}
void buildlist(node*&head)
{
int data;
cin>>data;
while(data!=-1)
{
insertattail(head,data);
cin>>data;
}
}
int main() {
nodehead=NULL;
buildlist(head);
reverse(head);
int pos;
cin>>pos;
nodem=lastelement(head,pos);
cout<data;
return 0;
}
Why fast is not pointing to 6. but it is pointing to 5
the code provided is passing all the test cases, can you elaborate the problem you are facing.
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.