Reverse Nodes in Groups of K
Updated
Problem statement
You are given head, the first node of a finite, acyclic singly linked list, and a positive integer k. Reverse the nodes in each complete group of k consecutive nodes and return the resulting head.
Start grouping at the original head. After reversing a complete group, continue with the next group. If fewer than k nodes remain at the end, keep that final partial group in its original order.
Reverse node order by changing next links. Keep every original node exactly once, with its original value unchanged, and return an acyclic list. Do not swap node values or create copied nodes for the result. A temporary dummy node is allowed if it is not part of the returned list.
The ListNode type is provided. Each node stores an integer value and a next-node reference. The examples display node values in list order; the function accepts and returns actual linked-list nodes.
Changing the original nodes' next links is intended. When k=1, returning the original head is valid. Use local working state for each call.
Function
reverseKGroup(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [1,2,3,4,5]k = 2return = [2,1,4,3,5]The first two complete groups are [1,2] and [3,4]. Reversing their nodes gives [2,1,4,3]. The final single node 5 is a partial group and stays at the end.
Example 2
head = [3,1,4,1,5,9,2]k = 3return = [4,1,3,9,5,1,2]The complete groups [3,1,4] and [1,5,9] become [4,1,3] and [9,5,1]. The final node 2 stays in place. The two nodes with value 1 remain distinct original nodes.
Example 3
head = [-2,0,5]k = 1return = [-2,0,5]Every group has one node, so all next links and the original head may stay unchanged. The result values are [-2,0,5].
Constraints
- If
nis the number of nodes,1 <= n <= 30. - Every node value is an integer between
-1000and1000, inclusive. Duplicate values are allowed. kis an integer with1 <= k <= 30andk <= n.- The input is one finite acyclic chain. The result contains the same nodes and unchanged values.
More Akuna Capital Phone Screen questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Sep 2026
- Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
- Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
- Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
- Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Greedy Person-Cake Matching DistanceSnowflake · PHONE SCREENSnowflake interview questions
- Least Frequently Used CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Checkpointed Dynamic SequenceOpenAI · PHONE SCREENOpenAI interview questions
- Merge k Sorted ListsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU Cache with Expiring EntriesPaytm · PHONE SCREENPaytm interview questions
- LFU CacheFreshworks · PHONE SCREENFreshworks interview questions
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- Reverse Nodes in K-GroupByteDance · PHONE SCREENByteDance interview questions
- LFU Cache with LRU Tie-BreakingRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Merge k Sorted ListsOracle · PHONE SCREENOracle interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)