dsa · medium
Reverse Linked List in Groups of K
GreyOrangeLinked List
Reverse nodes of head in groups of k. Leftover tail stays. Return the value list.
Arguments
head— the linked list, given as a Python list of node valuesk— group size — reverse every complete run ofknodes; leftover tail stays
**Example:** [1,2,3,4,5], k=2 → [2,1,4,3,5]. k=3 → [3,2,1,4,5].
Constraints
1 <= k <= length <= 500 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [1,2,3,4,5] 2 Expected: [2,1,4,3,5]
Example 2
Input: [1,2,3,4,5] 3 Expected: [3,2,1,4,5]