Flip Every Complete Block of K in a Sequence
Linked ListsAuto-graded · 17 test casesLast updated
Reverse each full consecutive block of k items in a list, leaving a shorter leftover tail unchanged; tests pointer-style reversal logic and boundary handling.
Flip Every Complete Block of K
You are given a sequence of integers, values, that represents a singly linked list from head to tail, and a positive integer k.
Split the sequence from the front into consecutive blocks of exactly k elements. Reverse the order of the elements inside every complete block. If the final block has fewer than k elements, leave it exactly as it appears in the input.
Return the resulting sequence as a new list. Do not modify the input list.
The problem is modeled on a linked list, where only the links between nodes may be rearranged and not the stored values. Your function receives and returns plain Python lists, but a strong solution should be able to handle the reversal at the pointer level.
Rules
- Blocks are formed starting from the first element, with no overlap and no gaps.
- Only blocks containing exactly
kelements are reversed. - Elements of a leftover shorter block keep their original order and position.
- If
k = 1, the output equals the input. - If
kis larger than the length of the list, the output equals the input.
Constraints
1 <= k <= n <= 5000, wherenis the length ofvalues.0 <= values[i] <= 1000.
Examples
Example 1
Input: flip_k_blocks([1, 2, 3, 4, 5], 2)
Output: [2, 1, 4, 3, 5]
Example 2
Input: flip_k_blocks([1, 2, 3, 4, 5], 3)
Output: [3, 2, 1, 4, 5]
Hints
Hint 1
Before reversing a block, check that at least k elements remain from the current position. If fewer remain, stop and keep the rest unchanged.
Hint 2
In a linked list, a dummy node placed before the head removes the special case where the first block changes the head. Track the node just before the current block, the block's last node, and the node just after it.
Hint 3
Reverse a block in place with the usual prev/curr walk, but start prev at the node after the block so the block's new tail is already connected to the rest. Then attach the block's new front to the node before it and move on.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Walk through the sequence one block at a time. At each position, check whether a full block of k elements is available. If so, reverse it; if not, stop, because the remaining shorter tail must stay as it is.
In the linked-list formulation, keep three pointers per block:
group_prev: the node right before the block (initially a dummy node ahead of the head, so the first block needs no special handling).kth: the last node of the block, found by advancingksteps. If this runs off the end, the block is incomplete and we stop.group_next: the node right after the block.
Reverse the block in place with the standard prev/curr loop, but start prev at group_next. Once the loop finishes, the block's old first node already points to group_next. Then set group_prev.next = kth and move group_prev to the old first node, which is now the block's tail.
For the list-based function required here, the same idea is applied directly to the array: for each start index 0, k, 2k, ... such that start + k <= n, reverse the slice result[start:start + k]. The range ends at n - k, so only complete blocks are touched.
Why it is correct
At the start of each step, everything before the current block is already in its final position. Reversing exactly the k elements of a complete block puts them in their final order and does not affect any other element. An incomplete trailing block is never reversed, as the rules require. Each step moves forward by k, so the process terminates and covers every complete block exactly once.
Complexity
- Time: O(n), since each element is examined and moved a constant number of times.
- Space: O(n) for the returned list copy. The pointer-based linked-list version uses O(1) extra space beyond the list itself.
Reference solution
from typing import List
def flip_k_blocks(values: List[int], k: int) -> List[int]:
result = values[:]
n = len(result)
for start in range(0, n - k + 1, k):
result[start:start + k] = reversed(result[start:start + k])
return result