I am preparing for a technical interview and I am stuck at writing this program to reverse every k nodes of a linked list.
For example
1->2->3
I would do something like this:
init curr (node pointer) to point to the beginning of the list.
while end of list is not reached (by curr):
- reverse(curr, k)
- advance curr k times
and reverse is a function that reverses the first k elements starting from curr.
this might not be the most elegant or the most efficient implementation, but it works and is quite simple.
to answer regarding the code you added:
you returned prev, which is constantly being advanced. you should return the beginning of the list.