Reverse Linked-List Nodes in Groups of K
Updated
Problem statement
Given the head of a singly linked list and an integer k, reverse the nodes in each complete consecutive group of k nodes. If the final group contains fewer than k nodes, leave it in its original order.
Change node links rather than swapping stored values. Return the new head. For this exercise, assume the list is nonempty, contains distinct values and has no cycle. The input and output display each list as an array, from its head to its tail.
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
Reverse each pair; the one-node tail stays unchanged.
Example 2
head = [1,2,3,4,5]k = 3return = [3,2,1,4,5]Moved or new
Only the first three nodes form a complete group.
Constraints
- For this exercise, assume
1 <= n <= 1000, where n is the number of nodes. 1 <= k <= n.- Node values are distinct integers in
[-10^9,10^9]. - A short final group is unchanged.
More PeopleStrong interview questions
- Unique Paths Through a GridOA · Seen Mar 2026
- Aggressive CowsOA · Seen Oct 2021
- Shortest Path in an Unweighted GraphOA · Seen Oct 2021
- Total Set Bits from 1 to NOA · Seen Oct 2021
- Swap Two Array ElementsOA · Seen Sep 2021
- Minimum Travel Time with Flashing Traffic LightsOA · Seen Aug 2021
- Permutation in a StringOA · Seen Aug 2021
- Rat in a Maze: All Simple PathsOA · Seen Aug 2021
- Add Two Binary StringsONSITE INTERVIEW · Seen Mar 2026
- Four SumONSITE INTERVIEW · Seen Mar 2026
Similar questions at other companies
- Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
- Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
- Count Nodes in a ChainApple · PHONE SCREENApple interview questions
- Detect a Cycle in a Linked ListSalesforce · PHONE SCREENSalesforce interview questions
- Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
- Reverse a Linked ListMygate · PHONE SCREENMygate interview questions
- Find the Intersection Node of Two Linked ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Find the Intersection Node of Two Linked ListsConcentric AI · OAConcentric AI interview questions
- Unique Target-Sum Pairs in a Sorted Doubly Linked ListArista Networks · PHONE SCREENArista Networks interview questions
- Linked List Palindrome in Constant SpaceNVIDIA · PHONE SCREENNVIDIA interview questions