25. Reverse Nodes in k-Group

Practice Reverse Nodes in k-Group for free on NudgeCode: Hard, Linked List pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

Given the head of a linked list, reverse the nodes in consecutive groups of k and return the new head. If the last group has fewer than k nodes, leave it as it is.

Change the links between nodes; do not just swap the values.

Example

head = [1,2,3,4,5], k = 2  ->  [2,1,4,3,5]

Constraints