Appending linked list(why is it not working though test case is passed)

#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *next;

};
void insert(node **n,int d){
node * temp=new node();
node * last=*n;
temp->data=d;
temp->next=NULL;
if(n==NULL){
n=temp;
return;
}
while(last->next!=NULL)
last=last->next;
last->next=temp;
return;
}
void print(node
n){
while(n!=NULL){
cout<<" "<data;
n=n->next;
}
}
node
change(node *&n,int t){

node *temp=n;int i=0;
    while(temp!=NULL && i<t){
        temp=temp->next;
        i++;
    }
    node*head=temp->next;
    temp->next=NULL;
return head;

}
int main(){
int n;cin>>n;
node *head=NULL;int f;
for(int i=0;i<n;i++)cin>>f,insert(&head,f);
int t;cin>>t;
node *om=change(head,n-t-1);
print(om);
print(head);
}

share your code using ide.codingblocks.com will debug your code and let you know.

Check now ->


Logic is not right but i have edited your code in such a way that it will pass all test cases.

what is the fault in the logic

Logic is also correct just have edited base cases which you can see.

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.