FastPrepReverse Linked-List Nodes in Groups of K
Problems/Reverse Linked-List Nodes in Groups of K

Reverse Linked-List Nodes in Groups of K

PeopleStrong logoPeopleStrong● HardFULLTIMEOAPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A 2021 report clearly gives reversal of nodes in groups of k, with a short last group left unchanged. A later report only says to swap nodes in k-pairs. This exercise is a compatible reading of that unclear phrase, not a confirmed exact match. We chose the function interface and the limits. Our version is about 95% the same as the clear report.

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) → ListNode

Examples

Example 1

head = [1,2,3,4,5]k = 2return = [2,1,4,3,5]
head
12345
→
return
21435
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]
head
12345
→
return
32145
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

Interview reports that mention this question

  • PeopleStrong SDE OA and Interview Questions: 12 Experiences
All PeopleStrong interview questionsSee PeopleStrong hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        // Write your solution here.
    }
}
Checking account…