Reverse Nodes in k-Group
Reverse the list k nodes at a time; a final group shorter than k stays as-is.
Intuition
Reverse-a-list is the classic three-pointer flip. Here we do it in bursts of k, but only after checking k nodes exist — otherwise the tail group must stay untouched. A dummy node plus a groupPrev anchor makes each spliced group connect cleanly.
Approach
Probe before you flip
From groupPrev, walk k steps to find kth. If you run off the list, the remaining group is short — stop.
Reverse the group
Standard prev/curr reversal of exactly the k nodes, with prev initialized to the node after the group so the flipped group already points onward.
Reconnect and advance
The group's old first node becomes its last; groupPrev.next = kth and groupPrev moves to that old first node. Repeat.
Solution & live demo
Edge cases
The probe fails on the last group and it is left in original order.
Every probe succeeds but reversal of one node is identity — list unchanged.