Reverse Nodes In K Group
Reverse each complete consecutive group of k nodes in a linked list. Leave a final group shorter than k untouched. Return the new head and change links rather than node values.
Example 1
head = [1,2,3,4,5]
k = 3[3,2,1,4,5]Explanation
Reverse complete group 1,2,3; the short remainder 4,5 stays ordered.
Example 2
head = [1,2,3,4]
k = 2[2,1,4,3]Explanation
Reverse both length-two groups independently into 2,1 and 4,3.
Example 3
head = [7]
k = 1[7]Explanation
Groups of one leave order unchanged.
Constraints
- 1 <= k <= number of nodes <= 5000
Function contract
reverseKGroupArguments: list, jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 35 cases, on your device.
[1,2,3,4,5]3