Given the head of a linked list, reverse the nodes of the list k at a time and return the modified list. If the number of nodes left at the end is smaller than k, leave those nodes in their original order. You may not change node values; only the links between nodes may be changed.
Input format for LiteCode:
Line 1: integer n, the number of nodes
Line 2: n space-separated node values
Line 3: integer k
Output format:
Print the resulting linked list as space-separated values on one line.
Examples
Input: 5
1 2 3 4 5
2
Output: 2 1 4 3 5
Input: 5
1 2 3 4 5
3
Output: 3 2 1 4 5
Constraints
The number of nodes in the list is n.
1 <= k <= n <= 5000
0 <= Node.val <= 1000
Loading...
Results
Not run
Runtime: --Memory: --
5
1 2 3 4 5
2
Reverse Nodes in k-Group
HardLinked ListRecursion
Given the head of a linked list, reverse the nodes of the list k at a time and return the modified list. If the number of nodes left at the end is smaller than k, leave those nodes in their original order. You may not change node values; only the links between nodes may be changed.
Input format for LiteCode:
Line 1: integer n, the number of nodes
Line 2: n space-separated node values
Line 3: integer k
Output format:
Print the resulting linked list as space-separated values on one line.