Reverse a Linked List in Groups of k
Updated
Problem statement
You are given the head of a singly linked list and an integer k. Starting from the head, cut the list into groups of k nodes; the last group may have fewer than k nodes.
Reverse the order of the nodes inside every group, including a shorter last group, keep the groups in their original order, and return the head of the resulting list. Rearrange the existing nodes instead of creating new ones.
Function
reverseInGroups(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [5,3,9,1,4,8,2]k = 3return = [9,3,5,8,4,1,2]Moved or new
The groups are [5, 3, 9], [1, 4, 8] and the shorter last group [2]. Each is reversed: [9, 3, 5], [8, 4, 1], [2].
Example 2
head = [10,20,30,40,50]k = 2return = [20,10,40,30,50]Moved or new
Groups [10, 20], [30, 40] and [50] become [20, 10], [40, 30] and [50].
Example 3
head = [6,7,8]k = 5return = [8,7,6]Moved or new
k is larger than the list, so the whole list is one short group, and it is reversed too.
Constraints
- The list has between
1and2000nodes. -1000 <= node.val <= 10001 <= k <= 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
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheGimlet Labs · PHONE SCREEN
- LRU CacheBNSF Railway · PHONE SCREENBNSF Railway interview questions
- Max Frequency TrackerProcol · PHONE SCREEN
- Balanced Session ManagerPalantir · PHONE SCREENPalantir interview questions
- LRU Cache Operation SequenceCARS24 · PHONE SCREEN
- LRU Key-Value Cache OperationsCohesity · PHONE SCREEN
- Merge Timestamped Record ListsNuro · PHONE SCREENNuro interview questions
- Least Recently Used CacheMicrosoft · OA · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
- Adobe Product Intern Interviews: 15 Experiences (Part 3 of 4)