Code is showing right output but when i am running on cb it is showing run time error

Ques:linked list -k append
code
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int d){
data=d;
next=NULL;
}
};
void print(node* head)
{
nodetemp=head;
while(temp!=NULL)
{
cout<data<<" ";
temp=temp->next;
}
}
void insert(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 append(node *&head,int k)
{
int ct;
node *h=head;
node *h2=head;
while(h!=NULL)
{
ct++;
h=h->next;
}
h=head;
node p=head;
node
m;
int jana=ct-k-1;
for(int i=1;i<=jana;i++)
{
p=p->next;

}
m=p->next;
p->next=NULL;
head=m;
while(true)
{
	if(m->next==NULL)
	{
	m->next=h2;
	break;
}
m=m->next;
}

}

int main()
{
node *head=NULL;
int n;
cin>>n;
while(n–)
{
int d;
cin>>d;
insert(head,d);
}
int k;
cin>>k;

append(head,k);

print(head);

}

hey @samadsid7, I have made changes to your code . You can check them here https://ide.codingblocks.com/s/93892.

Althogh your approach not optimized, I will suggest you to go through this one https://ide.codingblocks.com/s/66122

1 Like

hey @samadsid7, if your query is resolved. Please mark this doubt as resolved and rate me on the basis of your experience.
rating option will appear when to mark this doubt as resolved