Reverse Linked-List Groups of Alternating Sizes
Updated
Problem statement
You are given the head of a singly linked list and two group sizes x and y. Walk the list from the head and cut it into consecutive groups whose sizes take turns: the first group has x nodes, the second y, the third x, the fourth y, and so on.
Reverse the order of the nodes inside every full group. If the last group has fewer nodes than its size calls for, leave it in its original order. Return the head of the resulting list.
Function
reverseAlternatingGroups(head: ListNode, x: int, y: int) → ListNodeExamples
Example 1
head = [4,9,2,7,5,1,8]x = 2y = 3return = [9,4,5,7,2,8,1]The groups have sizes 2, 3, 2: [4, 9], [2, 7, 5] and [1, 8]. All three are full, so each is reversed: [9, 4], [5, 7, 2], [8, 1].
Example 2
head = [3,6,1,8,2,5,7]x = 3y = 2return = [1,6,3,2,8,5,7]The first group [3, 6, 1] becomes [1, 6, 3] and the second [8, 2] becomes [2, 8]. The third group should have 3 nodes but only [5, 7] are left, so it stays as it is.
Example 3
head = [10,20]x = 5y = 1return = [10,20]The first group needs 5 nodes but the list has only 2, so nothing is reversed.
Constraints
- The list has between
0and2000nodes. -1000 <= node.val <= 10001 <= x <= 20001 <= y <= 2000
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
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