K append numbers

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;
}
Can you explain the algo for this code

@dips123deepali_c25f140838182212
so basically lets understand with example let say linked list is 1->2->2->1->8->5->6 and k is 3 so output is 8->5->6->1->2->2->1.

so oldhead pointer is on 1 which is head right now and take 2 pointer fast and slow points to
head intially.

then Traverse linked list until the value of i less than K and next of fast pointer is not null increment fast pointer.
so now fast is at next 1 .
so then traverse slow and fast until fast->next!=NULL and fast!=NULL.
so Now fast is at last node and slow is at 1.
Now take newhead which points to next of slow which is at 1 and next of slow points to null… and then fast which is at 6 points to old head which is !st 1.
so output is 8->5->6->1->2->2->1.

if still not understand then show this

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.