Reverse Nodes in k-Group
Updated
Problem statement
You are given the head of a singly linked list and a positive integer k. Split the list into groups of k nodes, starting from the head, and reverse the order of the nodes inside each full group. Return the head of the changed list.
If the last group has fewer than k nodes, leave those nodes in their original order.
Follow-up: can you do it by changing the links in place, using only O(1) extra memory?
Function
reverseKGroup(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [1,2,3,4,5,6,7,8]k = 3return = [3,2,1,6,5,4,7,8]Moved or new
The first two groups of 3 are 1, 2, 3 and 4, 5, 6; they become 3, 2, 1 and 6, 5, 4. Only 7, 8 is left, which is fewer than 3 nodes, so it stays as it is.
Example 2
head = [5,9,2]k = 1return = [5,9,2]A group of one node reversed is the same node, so the list does not change.
Example 3
head = [4,3,2,1]k = 4return = [1,2,3,4]Moved or new
The whole list is exactly one group of 4, so the whole list is reversed.
Constraints
- The number of nodes
nis in the range[1, 5000]. 0 <= Node.val <= 10001 <= k <= n
More Microsoft Onsite Interview questions
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
- Totals from a Tiered Rate TableONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Reverse Nodes in K-GroupAmazon · ONSITE INTERVIEW
- Merge K Sorted Linked ListsThe Walt Disney Company · OA · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Merge K Sorted ListsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Merge K Sorted ListsSkydio · ONSITE INTERVIEWSkydio interview questions
- Reverse a Linked List in Groups of KExpedia · ONSITE INTERVIEWExpedia interview questions
- Merge K Sorted Linked Lists Into OneCapital One · ONSITE INTERVIEWCapital One interview questions
- Sorted Doubly Linked List to Balanced BST In PlaceSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- Reverse Nodes in K-GroupByteDance · PHONE SCREENByteDance interview questions
- Reverse a Linked ListCisco · PHONE SCREENCisco interview questions