Solution Review: Problem Challenge 1
We'll cover the following
Reverse alternating K-element Sub-list (medium) #
Given the head of a LinkedList and a number ākā, reverse every alternating ākā sized sub-list starting from the head.
If, in the end, you are left with a sub-list with less than ākā elements, reverse it too.
Solution #
The problem follows the In-place Reversal of a LinkedList pattern and is quite similar to Reverse every K-element Sub-list. The only difference is that we have to skip ākā alternating elements. We can follow a similar approach, and in each iteration after reversing ākā elements, we will skip the next ākā elements.
Code #
Most of the code is the same as Reverse every K-element Sub-list; only the highlighted lines have a majority of the changes: