PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Coding & Algorithms/Microsoft

Reverse linked list in fixed-size groups

Last updated: Mar 29, 2026

Quick Overview

This question evaluates proficiency with singly linked list manipulation, pointer/reference management, in-place group operations, and complexity reasoning including time and constant extra space.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Reverse linked list in fixed-size groups

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

### Problem You are given the head of a singly linked list and an integer `k` (`k >= 1`). Reverse the nodes of the list **in-place** in groups of size `k`: - The nodes in each consecutive block of `k` nodes should be reversed as a group. - If the number of nodes at the end of the list is **less than** `k`, leave those nodes in their original order. - Return the new head of the modified list. You may assume the list nodes are defined in the usual way, for example: - In C++: - `struct ListNode { int val; ListNode* next; };` - In Java: - `class ListNode { int val; ListNode next; }` #### Example Input list (values): `1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7`, and `k = 3` Output list should be: `3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7` - The first 3 nodes `(1, 2, 3)` are reversed to `(3, 2, 1)`. - The next 3 nodes `(4, 5, 6)` are reversed to `(6, 5, 4)`. - The last node `7` is fewer than `k` nodes, so it stays as is. #### Constraints - Number of nodes `n` can be up to around `10^5`. - You must operate in-place using `O(1)` extra space (excluding recursion stack if you choose a recursive solution). - Aim for `O(n)` time. Describe your algorithm and then implement a function that performs this transformation.

Quick Answer: This question evaluates proficiency with singly linked list manipulation, pointer/reference management, in-place group operations, and complexity reasoning including time and constant extra space.

Related Interview Questions

  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Sort Three Categories In Place - Microsoft (medium)
  • Retain Top K Elements - Microsoft (medium)
Microsoft logo
Microsoft
Oct 28, 2025, 12:00 AM
Software Engineer
Onsite
Coding & Algorithms
3
0

Problem

You are given the head of a singly linked list and an integer k (k >= 1).

Reverse the nodes of the list in-place in groups of size k:

  • The nodes in each consecutive block of k nodes should be reversed as a group.
  • If the number of nodes at the end of the list is less than k , leave those nodes in their original order.
  • Return the new head of the modified list.

You may assume the list nodes are defined in the usual way, for example:

  • In C++:
    • struct ListNode { int val; ListNode* next; };
  • In Java:
    • class ListNode { int val; ListNode next; }

Example

Input list (values):

1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7, and k = 3

Output list should be:

3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 7

  • The first 3 nodes (1, 2, 3) are reversed to (3, 2, 1) .
  • The next 3 nodes (4, 5, 6) are reversed to (6, 5, 4) .
  • The last node 7 is fewer than k nodes, so it stays as is.

Constraints

  • Number of nodes n can be up to around 10^5 .
  • You must operate in-place using O(1) extra space (excluding recursion stack if you choose a recursive solution).
  • Aim for O(n) time.

Describe your algorithm and then implement a function that performs this transformation.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.