Hard · Linked Lists

Reverse Nodes in k-Group

Given the head of a linked list and an integer k ≥ 1, reverse the nodes of each complete group of k consecutive nodes by relinking them (node values stay unchanged), leave a final group of fewer than k nodes in its original order, and return the new head.

Examples

Example 1

[1,2,3,4,5], k=2

Output: [2,1,4,3,5]

Example 2

[1,2,3,4,5], k=3

Output: [3,2,1,4,5]

Rebuild it in the studio

Open every published interview problem, solution, scene, and memory room free, with no account.

More Linked Lists problems