25. Reverse Nodes in k-Group
Asked at Walmart, TikTok
Difficulty
Hard
Time
O(n)
Space
O(1)
Pattern
Linked List
Companies
2
Problem
You are given the head of a singly linked list and a positive integer k. Reverse the list in blocks of k nodes at a time, then return the head of the new list.
A block means k nodes that are next to each other. You reverse each block internally, but the blocks themselves stay in the same order. If the last block has fewer than k nodes, leave those leftover nodes exactly as they are — do not reverse them.
You must relink the nodes, not change the values inside them. Try to use only O(1) extra memory (a few pointer variables), not an array of all nodes.
Reverse each block of k, keep the rest
Constraints
- ●
The number of nodes is n. - ●
1 <= k <= n <= 5000 - ●
0 <= Node.val <= 1000
Examples
head = [1,2,3,4,5], k = 2[2,1,4,3,5]head = [1,2,3,4,5], k = 3[3,2,1,4,5]Asked At
2 companies| Company | Difficulty | |
|---|---|---|
| Walmart | Hard | View all Walmart questions → |
| TikTok | Hard | View all TikTok questions → |
How to Think About It
Brute force: convert to array, reverse every k elements, and rebuild the list.
Count nodes first to determine how many full k-groups exist.
Reverse each k-group using iterative reversal with three pointers.
Connect the tail of the previous group to the head of the reversed group.
Optimal: count total nodes, then iteratively reverse exactly k nodes per group, linking groups together.
Optimal Approach
First count the total number of nodes to determine how many complete k-groups exist. Use a dummy head node to handle the case where the first group is reversed. For each group, reverse k nodes iteratively by maintaining prev, curr, and next pointers. After reversing a group, connect the previous group tail to the new head, and advance the tail pointer. Repeat for each full group. Time complexity is O(n) with O(1) extra space.
Count, then reverse k nodes at a time
What Trips People Up in Real Interviews
Clarify that only complete k-groups are reversed and leftover nodes remain as-is.
Mention the need to count total nodes first to know how many groups to reverse.
Explain the iterative reversal technique using prev, curr, and next pointers.
Discuss using a dummy head to simplify edge case handling for the first group.
Address the constant space requirement ruling out recursion stack as a solution.
Solution Code
O(n) timeclass Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
def count_nodes(node):
count = 0
while node:
count += 1
node = node.next
return count
total = count_nodes(head)
dummy = ListNode(0, head)
prev_group_tail = dummy
curr = head
while total >= k:
prev = None
group_start = curr
for _ in range(k):
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
prev_group_tail.next = prev
group_start.next = curr
prev_group_tail = group_start
total -= k
return dummy.nextFrequently Asked Questions
What is the Reverse Nodes in k-Group problem?
Given the head of a linked list, reverse the nodes k at a time and return the modified list. If the number of nodes is not a multiple of k, the remaining nodes at the end should stay in their original order. Only constant extra memory may be used.
How do you solve Reverse Nodes in k-Group?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Reverse Nodes in k-Group?
Reverse Nodes in k-Group is asked at Walmart, TikTok. It is a hard difficulty problem.
What are common mistakes on Reverse Nodes in k-Group?
- Clarify that only complete k-groups are reversed and leftover nodes remain as-is.
- Mention the need to count total nodes first to know how many groups to reverse.
- Explain the iterative reversal technique using prev, curr, and next pointers.
- Discuss using a dummy head to simplify edge case handling for the first group.
- Address the constant space requirement ruling out recursion stack as a solution.