Flatten a Linked List in K-Group Level Order
Linked ListsAuto-graded · 17 test casesLast updated
Tests pointer manipulation by collecting nodes from a singly linked list in chunks of k, then reversing the order within each chunk's values to produce a flat list.
Flatten a Linked List in K-Group Level Order
You are given a singly linked list represented as a Python list of integers (where lst[i] is the value of the i-th node, starting from the head), and a positive integer k.
Your task is to process the list in non-overlapping groups of k consecutive nodes, and for each group, reverse the values within that group before collecting them. Remaining nodes at the end that do not fill a complete group of k are kept in their original order (not reversed).
Return a single flat list of integers containing all values in the processed order.
Details
- Process groups left-to-right.
- Within each complete group of exactly
knodes, output the values in reversed order. - If the remaining tail has fewer than
knodes, output those values in their original order. - An empty input list returns an empty list.
Constraints
1 ≤ k ≤ 10000 ≤ len(lst) ≤ 2000-10^6 ≤ lst[i] ≤ 10^6
Examples
Example 1
Input: flatten_k_group_reversed([1, 2, 3, 4, 5, 6], 2)
Output: [2, 1, 4, 3, 6, 5]
Example 2
Input: flatten_k_group_reversed([1, 2, 3, 4, 5], 3)
Output: [3, 2, 1, 4, 5]
Example 3
Input: flatten_k_group_reversed([], 4)
Output: []
Hints
Hint 1
Split the list into chunks of size k — the last chunk may be smaller than k.
Hint 2
Reverse each chunk that has exactly k elements; leave shorter tail chunks untouched.
Hint 3
Concatenate the processed chunks in order to build the final result.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem reduces to a straightforward chunking-and-conditional-reversal task once we recognize the linked-list framing is captured entirely by the order of values in the input list.
Steps:
- Walk through the list with an index
i, stepping bykeach iteration. - Slice
lst[i : i+k]to get the current chunk. - If the chunk has exactly
kelements it is a complete group — emit the values in reversed order. - Otherwise (tail group, fewer than
kelements) — emit the values as-is. - Accumulate all emitted values and return.
Why it is correct:
Every element is visited exactly once. A chunk is only reversed when we confirm it is full (len(chunk) == k), which precisely captures "complete groups". The final short tail is passed through unchanged, matching the specification.
Complexity:
- Time: O(n) — each element is copied at most twice (once into the chunk, once into the result).
- Space: O(n) — for the output list; the intermediate chunk slice is O(k) and reused.
from typing import List
def flatten_k_group_reversed(lst: List[int], k: int) -> List[int]:
result = []
n = len(lst)
i = 0
while i < n:
chunk = lst[i:i + k]
if len(chunk) == k:
result.extend(reversed(chunk))
else:
result.extend(chunk)
i += k
return result