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

Core interview problems are free with a full runner and lessons; Pro opens the complete library.

More Linked Lists problems