I am unable to find the right approach, please provide a hint
Right approach?
Hey @goyalvivek The need of the question is to reverse every k nodes (where k is an input to the function) and k is a factor of size of List.
Reverse the first sub-list of size k.
1.1 While reversing keep track of the next node and previous node.
1.2 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
Now prev becomes the new head of the list (see the iterative method)
N = 9 & K = 3
Original LL is : [9 -> 4 -> 1] -> [7 -> 8 -> 3] -> [2 -> 6 -> 5]
After k Reverse : [1 -> 4 -> 9] ->[ 3 -> 8 -> 7] ->[ 5 -> 6 -> 2]
Now you just have to reverse them.
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.