LL - k Reverse problem


im failes to build the funtion even after watching the video

Anshul, are u asking for kReverse function ??? you are unable to build that ?

yes mam,

Anshul, you can followup this approach for your function as,
You need to maintain three pointers as current, prev and next, as you require for normal reversing the linked list… The only addition here would be that you will use a count variable initialised to 0 and you will keep updating the count inside the while loop, uptil a point where the count is less than k…and then once the linked list is reversed, you will again recursively call that function again for next set of values in the linked list…
Refer to this code snippet…

node *reverse(node *head,int k)
{
node *current=head;
node *prev=NULL;
node *next=NULL;
int count=0;
while(current!=NULL && count<k)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
count++;
}
if(next!=NULL)
{
head->next=reverse(next,k);
}
return prev;
}