Reverse k nodes linked list

node* rev(node*&head,int k)
{

 node*p=NULL;
 node*c=head;
 node* n=NULL;
int cnt=0;
while(c!=NULL and cnt<k)
    {
        n=c->next;
        c->next=p;
        p=c;
        c=n;
        cnt++;
    }
    
    if((n)!=NULL)
    {
        head->next=rev(n,k);
    }
    return p;

}

what is problem with this function?

Hey Karan, this function’s code looks fine, there can be one mistake try to pass the argument node* head instead of node*&head. If still it doesn’t work fine then share your whole code here, so that we can help you in debugging it properly.

1 Like

https://ide.codingblocks.com/s/47978
please debug it