In the test case 1 for Kappend linkedlist question i am getting a run error. Kindly guide me through this. my output is matching the i/p o/p file provided in the solutions

#include<bits/stdc++.h>
using namespace std;

struct node{
int data;
node *next;
node(int d){
data = d;
this->next = NULL;
}
};

node *insert(node *head, int n){
node *tail = head;

if(n>0){
	int data;
	cin >> data;
	node *temp = new node(data);
	head = temp;
	tail = temp;
	}

for(int i = 1; i < n; i++){
	int data;
	cin >> data;
	node *temp = new node(data);

	tail->next = temp;
	tail = temp;
	}
return head;

}

void print(node *head){
while(head!=NULL){
cout << head->data << " ";
head = head->next;
}
}

node *Kappend(node *head, int k){
node *newHead = head, *tail = head;

while(k--)
	tail = tail->next;
	
newHead = tail->next;
tail->next = NULL;

tail = newHead;

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

return newHead;

}

int main(){
node *head = NULL;
int n;
cin >> n;
head = insert(head, n);
int k;
cin >> k;
k = k%n;
head = Kappend(head, n-k-1);
print(head);
}

Hey @raginibhayana99 please share your code using ide.codingblocks.com will let you know issue.

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.

Check now ->

it works! thanks but do guide me what did you do in the code

Hello @raginibhayana99 you have raised the doubt again tell me what you have not understood.

i have commented what he has added in your code.

1 Like

thank you so much! i have understood

@raginibhayana99 if your query is cleared then you can mark this as resolved.

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.