K reverse problem


please help

Hello @bhattanurag426 what is the problem that you are facing?

Hello @bhattanurag426 i have checked your code:
first of all you are not incrementing i the while loop where you are checking for i<k.
and also in that loop when you are doing c->next=p that is also wrong acc to me because it is creating a kind of self loop in which NULL will not encountered.
could you please explain your approach so that we can discuss and find why it is going wrong.

sir i think my approach is wrong can you please help me out

@bhattanurag426 have you seen the editorial for this?

@bhattanurag426 check this:

THis problem can be solved using recursion. Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev .

  • head->next = reverse(next, k) ( Recursively call for rest of the list and link the two sub-lists )
  • Return prev ( prev becomes the new head of the list
    and here is the implementation for your help:
    https://ide.codingblocks.com/s/422687

https://ide.codingblocks.com/s/422760 sir output is correct now but still some mistake in printing

got it sir i used -> instead of space

@bhattanurag426just replace -> this with space and then your code will pass all the test cases:
https://ide.codingblocks.com/s/422894
Happy Learning!!

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.