sir my code is running properly but sir please tell me better code than this
Better solution for linked list-k append
node* appendK(node *head,int k){
node *oldHead = head;
node *fast = head;
node *slow = head;
for(long i=0;i < k && fast->next!=NULL ;i++){
fast = fast->next;
}
while(fast->next!=NULL && fast!=NULL){
fast = fast->next;
slow = slow->next;
}
node *newHead = slow->next;
slow->next = NULL;
fast->next = oldHead;
return newHead;
}
This is smaller and more clear code of function that’ll perform appending.
sir after writing your code i am failing i test case ,showing as wrong answer, i updated the code in the same link ,please check again sir
you need to print head if k == 0, else you need to call appenK(head, k). so add an if statement before appendK and your code should work fine.
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.