#25
Reverse Nodes in k-Group
expert · 1070 · lc hard +32 · verified · 65.5% accepted · 15,559 likes · top 70%
Description
Reverse the nodes of linked list head in consecutive groups of k and return the new head. Any trailing nodes that do not fill a complete group of k remain in their original order. Node values may not be changed.
Example 1:
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Example 2:
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Code
1
2
3
4
5
6
7
8