98. Reverse Nodes in k-Group
hardAsked at VercelReverse every k consecutive nodes in a linked list. Vercel asks this for the disciplined pointer surgery — same skill as chunking a stream into fixed-size batches for their edge-function fan-out.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-Q4)— Vercel platform onsite; reserved for senior interviews.
- Blind (2026-Q1)— Listed as a Vercel hard.
Problem
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should remain as it is. You may not alter the values in the list's nodes, only nodes themselves may be changed.
Constraints
The number of nodes in the list is n.1 <= k <= n <= 50000 <= Node.val <= 1000
Examples
Example 1
head = [1,2,3,4,5], k = 2[2,1,4,3,5]Example 2
head = [1,2,3,4,5], k = 3[3,2,1,4,5]Approaches
1. Convert to array, reverse k-chunks, rebuild
Dump to array, reverse in-place per chunk, rebuild list.
- Time
- O(n)
- Space
- O(n)
// Allocates new nodes. Vercel wants in-place.Tradeoff: Extra space.
2. Iterative in-place with group reversal (optimal)
For each group of k: check if k nodes remain; if so, reverse them and splice. Use dummy head for the first group.
- Time
- O(n)
- Space
- O(1)
function reverseKGroup(head, k) {
const dummy = { val: 0, next: head };
let groupPrev = dummy;
while (true) {
let kth = groupPrev;
for (let i = 0; i < k && kth; i++) kth = kth.next;
if (!kth) break;
const groupNext = kth.next;
// Reverse group between groupPrev.next ... kth
let prev = groupNext, cur = groupPrev.next;
while (cur !== groupNext) {
const next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
const tmp = groupPrev.next;
groupPrev.next = kth;
groupPrev = tmp;
}
return dummy.next;
}Tradeoff: In-place, O(1) extra. The trick: for each k-group, find the kth node; if it exists, reverse the segment. groupPrev advances to the post-reversal end of the group.
Vercel-specific tips
Vercel grades the in-place iterative version. Bonus signal: explaining how the inner reversal terminates (when cur === groupNext, the segment is fully reversed) and noting the four pointer roles: groupPrev (anchor before), kth (last in group), groupNext (first after), and the standard prev/cur for in-loop reversal.
Common mistakes
- Not checking if k nodes remain — would partially reverse a tail.
- Wrong terminator on the inner reverse — must stop at groupNext, not null.
- Forgetting to advance groupPrev to the END of the just-reversed group.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Recursive variant.
- Reverse alternating k-groups.
- Rotate list by k (LC 61).
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why use kth as a probe?
We need to know if k nodes remain BEFORE reversing. Walking k steps from groupPrev lets us bail out cleanly if not. If kth becomes null, fewer than k nodes remain — leave them as-is.
Why save tmp = groupPrev.next before reassigning?
After the reversal, the OLD head of the group (groupPrev.next before reversal) is now the LAST element of the reversed group. We need this to advance groupPrev to the new group's anchor.
Practice these live with InterviewChamp.AI
Drill Reverse Nodes in k-Group and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →