Reverse Nodes in K-Group
Updated
Problem statement
Given the head head of a singly linked list and a positive integer k, reverse the nodes of the list in consecutive groups of exactly k nodes.
If fewer than k nodes remain at the end, leave that final group unchanged. Return the head of the transformed list.
You may change node links, but you must not change node values.
Function
reverseKGroup(head: ListNode, k: int) → ListNodeExamples
Example 1
head = [1,2,3,4,5]k = 2return = [2,1,4,3,5]Moved or new
The complete groups [1, 2] and [3, 4] are reversed. The final one-node group remains unchanged.
Example 2
head = [1,2,3,4,5]k = 3return = [3,2,1,4,5]Moved or new
The first three nodes form one complete group and are reversed. Only two nodes remain, so their order is preserved.
Constraints
- The list contains between
1and5000nodes. -1000 <= node.val <= 10001 <= k <= number of nodes- The input list is finite and contains no cycle.
More ByteDance Phone Screen questions
- Count N-Queens PlacementsPHONE SCREEN · Seen Nov 2026
- Matrix EscapePHONE SCREEN · Seen Nov 2026
- Largest Number Below N from Reusable DigitsPHONE SCREEN · Seen Nov 2026
- Log Analyzer: Busiest Service in a Rolling WindowPHONE SCREEN · Seen Nov 2026
- Majority ElementPHONE SCREEN · Seen Nov 2026
- Valid ParenthesesPHONE SCREEN · Seen Nov 2026
- Binary Tree Target-Sum PathsPHONE SCREEN · Seen Nov 2026
- Longest Increasing SubsequencePHONE SCREEN · Seen Nov 2026
- Alien DictionaryPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Lexicographically Smallest Topological OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Merge K Sorted Linked ListsBharatPe · PHONE SCREEN
- Merge K Sorted ListsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Convert a Sorted Linked List to a Balanced BSTMeta · PHONE SCREENMeta interview questions
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Reverse a Linked ListCisco · PHONE SCREENCisco interview questions
- Reverse a Linked ListApple · PHONE SCREENApple interview questions
- Reverse a Singly Linked ListChubb · PHONE SCREENChubb interview questions
- Reverse a Singly Linked ListAdobe · PHONE SCREENAdobe interview questions
- Swap Adjacent Linked List NodesNutanix · PHONE SCREENNutanix interview questions
- Reverse a Singly Linked ListExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
ByteDance interview experiences
- ByteDance TikTok USDS SWE Intern Interview: 1 Experience
- ByteDance MLE New Grad Interviews: 1 Experience
- ByteDance Security SRE Intern Phone Screens: 1 Experience
- ByteDance USDS New Grad SWE Phone Screen: 1 Experience
- ByteDance New Grad Data Engineer Video Interviews: 1 Experience
- ByteDance Trust & Safety Research Scientist Intern: 1 Experience