Linked List - K Append

Please tell where my code is incorrect as some of the test cases are giving run error

the code is as follows:-

#include
using namespace std;

// defining the node class having the data and next pointer
class node{
public:
long long int data;
node* next;

node(long long int d){
    data=d;
    next=NULL;
}

};

// function to insert the elements at the head
void insertAtHead(node* &head,long long int data){
node* n=new node(data);
n->next=head;
head=n;
}

// function to insert node at last
void insertAtLast(node* &head,long long int d){
if(head==NULL){
insertAtHead(head,d);
}
else{
node * temp=head;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=new node(d);
}

}

// function to print all the elements of the linked list
void print(node* head){
while(head!=NULL){
cout<data<<" ";
head=head->next;
}
cout<<endl;
}

void appendK(node* &head,long long int k){
nodefast=head;
node
slow=head;
while(k>=1){
fast=fast->next;
k–;
}

node*sprev;
node*fprev;

while(fast!=NULL){
    fprev=fast;
    fast=fast->next;
    sprev=slow;
    slow=slow->next;
}
fprev->next=head;
head=slow;
sprev->next=NULL;

}

int main() {
long long int n1;
cin>>n1;
node* head1=NULL;
long long int data;
for(long long int i=1;i<=n1;i++){
cin>>data;
insertAtLast(head1,data);
}

long long int k;
cin>>k;

appendK(head1,k);
print(head1);

}

Hello @krikhi,

Please share your code using Online Coding Blocks IDE.
The way you have used may introduce syntax errors to it.
STEPS:

  1. Paste your original code on https://ide.codingblocks.com/
  2. Save it there.
  3. Share the URL generated.

I have send the link of the code. Please help in finding the error.

Hello @krikhi,

You have missed an important part of the question: K can be greater than N.

  1. To handle the case when k>=N:
    do K=K%N;
    This will, normalize the value of K to (0-N-1).
    Example:
    7
    1 2 2 1 8 5 6
    8
    Expected Output:
    6 1 2 2 1 8 5
    Your Output:
    1 2 2 1 8 5 6
    Observation:
    The required output is the same as that of k=1.
    Similarly,
    for k=7 the output is the same as that of k=0.
    for k=9 the output is the same as that of k=2.

  2. For the case when k=N,2*N,… so on.
    just return from the function.
    Reason:
    The linked list should remain unchanged.

I have modified your code:

Hope, this would help.
Give a like if you are satisfied.

I got it. thanks a lot