Code
Loading editor...
Tap Analyze to see visualization
Click Analyze to visualize
See step-by-step execution, variables, and output
Variables
Run code to see variables
Output
Console output will appear here
Click Analyze to visualize
See step-by-step execution, variables, and output
Run code to see variables
Console output will appear here
Use a dummy node and process the list in groups of k. For each group: (1) check if k nodes remain by advancing a pointer k times, (2) reverse the k nodes in place, (3) connect the reversed group to the previous group tail. If fewer than k nodes remain, leave them as-is.
Each node is visited at most twice (once to count, once to reverse). The groupPrev pointer tracks where the reversed group should be attached, ensuring groups connect correctly. Leftover nodes stay in original order.
Updated Feb 2026