I am getting run error

i think that my code is exceeding the time limit.

hello @anyamaurya
image
n is not initialised and u are still using it to access n->next
so check this

i am getting tle now

yeah u will get tle because again n again u r reinitialising i with 0

#include<bits/stdc++.h>
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*temp=head;
while(temp->next!=NULL)
{
	temp=temp->next;
}
temp->next=new node(data);
return;

}
void buildList(node*& head,int n)
{
int data;
cin>>data;

while(n--)
{
	insertAttail(head,data);
	cin>>data;
}

}
void reverse(node*& head,int k)
{
node* current=head;
node* prev=NULL;
node *n=current->next;
int i=0;
while(i<k)
{
n->next=current;
current->next=prev;
current=n;
prev=current;
i++;
if(i==k)
i=0;

}
return;

}
void print(node* head)
{
while(head!=NULL)
{
cout<data<<"–>";
head=head->next;
}
cout<<endl;
}
int main()
{
node *head=NULL;
int n,k;
cin>>n>>k;
buildList(head,n);
reverse(head,k);
print(head);
return 0;
}

so what should i do to make the correction

pls refer this->https://www.geeksforgeeks.org/reverse-a-list-in-groups-of-given-size/

after compilation ,i am getting correct output as
Provide Custom Input
Compile and Test Submit Code
9 3
9 4 1 7 8 3 2 6 5
Compilation Successfull
1–>4–>9–>3–>8–>7–>5–>6–>2–>
like this but when i am submitting it then it is giving me wrong answer

pls share ur updated code using cb ide

#include<bits/stdc++.h>
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*temp=head;
while(temp->next!=NULL)
{
	temp=temp->next;
}
temp->next=new node(data);
return;

}
void buildList(node*& head,int n)
{
int data;
cin>>data;

while(n--)
{
	insertAttail(head,data);
	cin>>data;
}

}
node * reverse(node* &head,int k)
{
node* current=head;
node* prev=NULL;
node n=NULL;
int i=0;
while(current!=NULL && i<k)
{
n=current->next;
current->next=prev;
prev=current;
current=n;
i++;
}
if(n!=NULL)
head->next=reverse(n,k);
return prev;
}
void print(node
head)
{
while(head!=NULL)
{
cout<data<<"–>";
head=head->next;
}
cout<<endl;
}
int main()
{
node head=NULL;
int n,k;
cin>>n>>k;
buildList(head,n);
node
ans=reverse(head,k);
print(ans);
return 0;
}

save it here-> https://ide.codingblocks.com/
and then share the generated link with me

image
use " " in place of -->
rest eveerthing is correct in ur code

but why is it so?why we are adding "–>"in between the two nodes.

In output format they have space in place of -->

okay i got that . Thank you