Dodomeki
Sign in

105. Reverse Blocks of k

Hard

Given a linked list and a positive integer k, reverse the nodes in each complete block of k nodes and return the head of the list. If the last block has fewer than k nodes, leave that part unchanged.

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, 6], k = 3
Output: [3, 2, 1, 6, 5, 4]
Constraints
  • 0 <= tamanho da lista <= 10000
  • 1 <= k <= 10000.
Linked list
Caso 1
head =
[1,2,3,4,5]
k =
2
Expected =
[2,1,4,3,5]
Caso 2
head =
[1,2,3,4,5,6]
k =
3
Expected =
[3,2,1,6,5,4]

Run checks these 2 cases. Submit runs all 7, including hidden ones.