
Problem Challenge 1: Reverse alternating K-element Sub-list (medium)
Problem Statement
Given the head of a LinkedList and a number k, reverse every alternating k sized sub-list starting from the head.
The first k nodes are reversed, the next k are left alone, the next k are reversed, and so on.
If the last group holds fewer than k nodes, treat it the same way as a full group: reverse it if it
falls on a reversing turn, leave it alone if it falls on a skipping turn.
Example:
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> null, k = 3
Output: 3 -> 2 -> 1 -> 4 -> 5 -> 6 -> 9 -> 8 -> 7 -> 10 -> null
Nodes 1 to 3 are reversed, 4 to 6 are skipped, 7 to 9 are reversed, and the final group holds only node 10. That group falls on a skipping turn, so it stays where it is.
Constraints:
- The number of nodes in the list is n.
1 <= n <= 50001 <= k <= 5000(kmay be larger thann)0 <= Node.val <= 1000
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory