I face run error except 2,6 test case.my code is here

#include
using namespace std;
struct node{
long long int data;
struct node *next;
};
struct node *p;
void addtrail(node &head,long long int data){
node
n=new node;
n->data=data;
n->next=NULL;
if(head==NULL){

head=n;
p=n;
}
p->next=n;
p=n;

}
void kalternate(node &head,long long int k,long long int n){
node
cur=head;
nodel;
int count=0;
while((cur)&&(count<n-k)){
l=cur;
cur=cur->next;
count=count+1;
}
node
t=cur;

while(cur->next){
    
    cur=cur->next;
}
 cur->next=head;
 l->next=NULL;
 head=t;

}
int main() {
long long int n,k;
cin>>n;
long long int a[n];
node *head=NULL;
for(long long int i=0;i<n;i++){
cin>>a[i];
addtrail(head,a[i]);
}

cin>>k;
kalternate(head,k,n);
node *u=head;
while(u){
cout<data<<" ";
u=u->next;
}
return 0;
}

Hey, you are facing runtime error because k could be greater than n and you have not accounted for that. In your code if k is greater than n, l (pointer to node) remains un-assigned and later you try to access it causing it to throw SIGSEGV(run error)

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.