Segmentation fault

I tried writing the above code but it is giving segmentation fault
can you please help me correct this?

u need to add tail the list to the head of older list ,which u r not doing.
make these two changes and it will work.

also do k=k%n for making value of k < n.

for refrence ->

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;
}

I made the changes: https://ide.codingblocks.com/s/364586 It is still giving segmentation fault

checking …

https://ide.codingblocks.com/s/364613 consider this code i made a mistake in the main earlier, I corrected that still it gives segmentation fault

Mentoned the changes in comments : https://ide.codingblocks.com/s/364620 :slight_smile:

Note K can be greater than N so to handle that I have added 2-3 lines