The last group must stay.
transcript
Reverse every three nodes. The chain has five nodes. What happens to the last two?
One to five, with k equal to three. Group one is one, two, three. Group two would be four and five. A full group has exactly three nodes.
Walk three nodes from the start. Three nodes remain. Reverse them. The chain reads three, two, one, four, five. Always check before you turn anything.
Walk three nodes from four. Five, and then nothing. Fewer than three remain. Stop. This is the check that saves you.
A loop that reverses what remains turns four and five too. The chain reads three, two, one, five, four. A short group must stay in order.
Three steps for every group. Check that k nodes remain. Then reverse the group. Then link it to the next group.
Check, reverse, link. A short group stays. Try it in the sandbox. Full breakdown in the lecture.