Reverse Each Group of K List Nodes
Updated
Problem statement
Give the n list nodes the positions 0 to n - 1, with position 0 at head, and group them by the block size k:
- Block
icovers positionsi * kthroughi * k + k - 1, fori = 0, 1, 2, .... Only the last block may come up short ofknodes. - A block holding
knodes keeps the same positions, but its nodes fill them in reverse order. - A short last block is not changed.
Return the node at position 0 after the change.
Function
reverseInGroups(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [4,-1,7,2,9,6,3,8,5,0]k = 4return = [2,7,-1,4,8,3,6,9,5,0]Moved or new
- Blocks of 4:
[4, -1, 7, 2]|[9, 6, 3, 8]|[5, 0] - Full blocks reversed:
[2, 7, -1, 4]|[8, 3, 6, 9]|[5, 0](the last block holds only 2 of 4 nodes, so it is unchanged) - Joined:
[2, 7, -1, 4, 8, 3, 6, 9, 5, 0]
Example 2
head = [11,-3,0,8,-6,14]k = 2return = [-3,11,8,0,14,-6]Moved or new
- Blocks of 2:
[11, -3]|[0, 8]|[-6, 14] - Full blocks reversed:
[-3, 11]|[8, 0]|[14, -6] - Joined:
[-3, 11, 8, 0, 14, -6]
Example 3
head = [-5,-12,-1,-9,-3,-7,-20]k = 3return = [-1,-12,-5,-7,-3,-9,-20]Moved or new
- Blocks of 3:
[-5, -12, -1]|[-9, -3, -7]|[-20] - Full blocks reversed:
[-1, -12, -5]|[-7, -3, -9]|[-20](the last block has 1 node, so it is unchanged) - Joined:
[-1, -12, -5, -7, -3, -9, -20]
Constraints
- The number of nodes
nis between1and1000. -1000 <= node.val <= 10001 <= k <= n
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- LFU CacheAmazon · ONSITE INTERVIEW
- LFU CacheJP Morgan Chase · ONSITE INTERVIEWJP Morgan Chase interview questions
- LFU CacheSnowflake · OA · ONSITE INTERVIEWSnowflake interview questions
- LRU Cache with TTLAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Recover a Function-Call LRU Cache with a Write-Ahead LogAnthropic · ONSITE INTERVIEWAnthropic interview questions
- Merge k Sorted ListsZS · ONSITE INTERVIEWZS interview questions
- LFU CacheSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Max StackLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- LFU CacheBlinkit · ONSITE INTERVIEWBlinkit interview questions
- Fair Multi-Tenant CacheOkta · ONSITE INTERVIEWOkta interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences