Linked list k appendd

please tell me whats wrong

here is the code–

#include<bits/stdc++.h>

using namespace std;

class node{

public:
    long long int data;
    node* next;
    node(long long int x){
        data=x;
        next=NULL;
    }

};

node* insert(node* head,long long int data){
node* temp=new node(data);
node* h;
h=head;
while(h!=NULL&&h->next!=NULL)
h=h->next;
if(h==NULL){
h=temp;
head=h;
}
else
h->next=temp;

return head;

}

void func(node* &head,int p,int n){
if(p<1||p>=n-1)
return ;
node *temp=head,*ptr,*pt;
while(p–)
temp=temp->next;

ptr=temp->next;
pt=ptr;
while(pt->next!=NULL)
pt=pt->next;

pt->next=head;
head=ptr;
temp->next=NULL;

}

int main(){

long long int t;

// cin>>t;
//while(t–){

long long int n;
cin>>n;

node *head1=NULL,*head2=NULL;
long long int x;
for(long long int i=0;i<n;i++){
    cin>>x;
    head1=insert(head1,x);
}

int k;
cin>>k;

func(head1,n-k-1,n);

while(head1!=NULL){
    cout<<head1->data<<" ";
    head1=head1->next;
}
cout<<endl;

//}
return 0;

}

Just a suggestion Prajawal
Please do not pass on the entire code without adding comments in it otherwise it becomes very difficult to detect the mistake without doing a dry run of entire code.
Tell details like what you have done(your approach),which all test cases are failing? Are the failing test cases showing run time error or wrong answer.
Please provide the above details :slight_smile:

here’s the new commented code…

2 test cases are passing…2 are failing…
failing ones are wrong answer…

hope you can reply soon…

Hey there
Your code is perfectly fine, just add the bold line:
k=k%n;
func(head1,n-k-1,n);//passing through the function
In the main function to deal with the case when k>n.
All the test cases will pass this way !!
Do let me know for further doubts in this question/approach else kindly mark the doubt as resolved :slight_smile:

thanks…will u see my this doubt too?? :no_mouth::no_mouth:

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.

Hii Prajawal


Here is your corrected code for merging linked list.
Again there were some small mistakes rest everything was fine!
I have mentioned the mistakes in form of comments.
Also note that merge is a reserved keyword so try avoid using it for function names.
All the best :slight_smile: