PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates skills in manipulating singly linked lists, pointer reassignments, and performing in-place group transformations while meeting time and space complexity constraints.

  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Reverse Nodes in K-Sized Groups

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Interview Round: Technical Screen

Given the head of a singly linked list and an integer `k`, reverse the nodes of the list in contiguous groups of size `k`. Requirements: - Only complete groups of `k` nodes should be reversed. - If the number of remaining nodes is fewer than `k`, leave those nodes in their original order. - You may change node pointers, but you should not modify node values. - Aim for `O(n)` time complexity and `O(1)` extra space. Example: - Input: `1 -> 2 -> 3 -> 4 -> 5`, `k = 2` - Output: `2 -> 1 -> 4 -> 3 -> 5` Example: - Input: `1 -> 2 -> 3 -> 4 -> 5`, `k = 3` - Output: `3 -> 2 -> 1 -> 4 -> 5`

Quick Answer: This question evaluates skills in manipulating singly linked lists, pointer reassignments, and performing in-place group transformations while meeting time and space complexity constraints.

Given the head of a singly linked list and an integer k, reverse the nodes of the list in contiguous groups of size k. Only complete groups of k nodes should be reversed. If the number of remaining nodes is fewer than k, leave those nodes in their original order. You may change node pointers, but you should not modify node values. For this platform, the linked list is represented as an array of values from head to tail, and your function should return the final linked list in the same array form.

Constraints

  • 0 <= len(values) <= 100000
  • 1 <= k <= 100000
  • -1000000000 <= values[i] <= 1000000000

Examples

Input: ([1,2,3,4,5], 2)

Expected Output: [2,1,4,3,5]

Explanation: The list is reversed in pairs: (1,2) becomes (2,1), (3,4) becomes (4,3), and 5 remains as is.

Input: ([1,2,3,4,5], 3)

Expected Output: [3,2,1,4,5]

Explanation: The first three nodes form a complete group and are reversed. The last two nodes are fewer than k, so they stay unchanged.

Input: ([1,2], 3)

Expected Output: [1,2]

Explanation: There are fewer than 3 nodes total, so no reversal happens.

Input: ([], 4)

Expected Output: []

Explanation: An empty list remains empty.

Input: ([7], 1)

Expected Output: [7]

Explanation: Groups of size 1 do not change the list.

Input: ([-1,-1,2,3,4,5], 4)

Expected Output: [3,2,-1,-1,4,5]

Explanation: The first four nodes are reversed as one group. The remaining two nodes are left unchanged.

Hints

  1. Use a dummy node so reconnecting the previous part of the list to a reversed group is easier.
  2. Before reversing a group, first check whether there are at least k nodes remaining.
Last updated: Jun 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,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.

Related Coding Questions

  • Solve Stack and String Shift Problems - Bytedance (medium)
  • Find LCA With Parent Pointers - Bytedance (medium)
  • Count Target-Sum Paths in an N-ary Tree - Bytedance (hard)
  • Connect Points With Minimum Total Distance - Bytedance (medium)
  • Minimize Increments to Equalize Path Costs - Bytedance (medium)