What is wrong with my approach?
hey @Anchal, your approach is not right, you just have to reverse first x elements and connect its last node to first node of next x nodes.
here is the logic for that
node* reverse(node* head,int k)
{
node* temp=head;
int cnt=1;
while(cnt<=k)
{
temp=temp->next;
++cnt;
}
//cout<<endl<<temp->data;
node* current=head;
node* previous=NULL;
node* nextNode;
while(current!=temp)
{
nextNode=current->next;
current->next=previous;
previous=current;
current=nextNode;
}
if(nextNode!=NULL)
{
head->next=reverse(nextNode,k);
}
return previous;
}
feel free to ask in case of further query.
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.