Linklist k append two test case fails

https://ide.codingblocks.com/s/57003 please debug the code

Hey Neeraj, you are not taking input correctly, it is mentioned that firstly you will get the size N, then N space separated elements and number of elements that are to be appended(n). So, update your main function like this

 int main(){
       node*head=NULL;
       int a;
       cin>>a;
       buildlist(head,a);
       int d;
       cin>>d;
       int jumps=d%a;
       head=append(head,jumps);
       print(head);
    }

Your append() is also not correct as you have written an infinite loop inside it at line:41

while(head!=NULL){
    tail=tail->next;
}

Check your logic for append() again.

ok i will try then i will tag you if is it didn’t work

https://ide.codingblocks.com/s/57123 please debug the code out of 4 test case only one is passed

@sanjeetboora please debug the code .

@sanjeetboora as you said i did the change but still two test case fails

Hey Neeraj, your logic for append() is not correct as you are supposed to append last K nodes in front but you are doing its reverse as you are appending the K front nodes at the last. Also update your code’s line 35 and 38 and start the loop from i=1, otherwise you will get an extra jump.

so what should I do for appending the lists

Its pretty simple to append K las nodes in front, you can do it like this

node* kAppend(node*head,int k,int n){
    node*last;
    last = head;
    int i=0;
    k = k%n;
    int ok = n-k-1; 
    while(last != NULL && i<ok){
        last = last -> next;
       i++;
    }
    node*second = last->next;
    last->next = NULL;
    return second;
}