PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to manipulate linked list pointers in place, handling in-group reversal while preserving overall structure. It tests practical application of data structure manipulation and edge-case handling, a common coding and algorithms interview topic assessing pointer-rewiring skill and space-efficient thinking.

  • medium
  • Bytedance
  • Coding & Algorithms
  • Software Engineer

Reverse Nodes in Groups of K

Company: Bytedance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

# Reverse Nodes in Groups of K You are given the `head` of a singly linked list and a positive integer `k`. Reverse the nodes of the list **`k` at a time** and return the head of the modified list. Process the list in consecutive blocks of `k` nodes: - Reverse the first `k` nodes, then the next `k` nodes, and so on. - If the number of remaining nodes at the end is **fewer than `k`**, leave those leftover nodes in their original order (do **not** reverse a partial group). Only the links between nodes may be changed; the values inside the nodes must stay attached to their original nodes (i.e. rewire pointers rather than copy values, ideally). ### Node definition ``` Node { int val Node next // null for the last node } ``` ### Input / Output - **Input:** the `head` node of the list, and an integer `k`. - **Output:** the `head` node of the list after every full group of `k` nodes has been reversed. ### Examples Given `k = 2` and the list `1 -> 2 -> 3 -> 4 -> 5`: ``` Input: 1 -> 2 -> 3 -> 4 -> 5 Output: 2 -> 1 -> 4 -> 3 -> 5 ``` The pairs `(1,2)` and `(3,4)` are each reversed; the final node `5` is alone (fewer than `k` nodes left), so it stays in place. Given `k = 3` and the list `1 -> 2 -> 3 -> 4 -> 5`: ``` Input: 1 -> 2 -> 3 -> 4 -> 5 Output: 3 -> 2 -> 1 -> 4 -> 5 ``` The first three nodes are reversed; the trailing `4 -> 5` is a partial group of size 2 (< 3) and is left untouched. Edge case — when `k = 1`, the list is returned unchanged. ### Constraints - The number of nodes `n` satisfies `1 <= n <= 5 * 10^4`. - `1 <= k <= n`. - Each node value fits in a 32-bit signed integer. - Target `O(n)` time. An in-place solution achieves `O(1)` extra space (excluding recursion stack). ### Follow-up Could you solve the problem using only `O(1)` extra memory, i.e. without recursion or an auxiliary buffer of nodes?

Quick Answer: This question evaluates a candidate's ability to manipulate linked list pointers in place, handling in-group reversal while preserving overall structure. It tests practical application of data structure manipulation and edge-case handling, a common coding and algorithms interview topic assessing pointer-rewiring skill and space-efficient thinking.

You are given a singly linked list, provided as an array `values` holding the node values in order from head to tail, and a positive integer `k`. Reverse the nodes of the list `k` at a time and return the resulting sequence of node values. Process the list in consecutive blocks of `k` nodes: reverse the first `k` nodes, then the next `k` nodes, and so on. If the number of remaining nodes at the end is fewer than `k`, leave those leftover nodes in their original order (do NOT reverse a partial group). Only the links between nodes may be changed; conceptually you should rewire pointers rather than copy values. Rebuild the list into an array of values (head to tail) for the return value. Examples: - values = [1,2,3,4,5], k = 2 -> [2,1,4,3,5] (pairs (1,2) and (3,4) reversed; lone 5 stays). - values = [1,2,3,4,5], k = 3 -> [3,2,1,4,5] (first three reversed; trailing 4->5 is a partial group and is left untouched). - When k = 1, the list is returned unchanged. Function signature: `solution(values, k)` returns the reordered list of values.

Constraints

  • 1 <= n <= 5 * 10^4 where n is the number of nodes
  • 1 <= k <= n
  • Each node value fits in a 32-bit signed integer
  • Target O(n) time
  • Only rewire links; a full group of k is reversed, a trailing partial group of size < k is left in original order

Examples

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

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

Explanation: Pairs (1,2) and (3,4) are each reversed; the final node 5 is alone (fewer than k left) so it stays in place.

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

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

Explanation: The first three nodes are reversed; the trailing 4->5 is a partial group of size 2 (< 3) and is left untouched.

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

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

Explanation: k = 1 reverses groups of one node, which leaves the list unchanged.

Input: ([1], 1)

Expected Output: [1]

Explanation: A single-node list is returned unchanged.

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

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

Explanation: Two full groups of three; both are reversed and there is no partial tail.

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

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

Explanation: k equals n, so the entire list is one full group and is fully reversed.

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

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

Explanation: Negative and large values reverse correctly in pairs: (-1,-2) and (-3,100); trailing 5 stays.

Hints

  1. Before reversing a group, first confirm there are at least k nodes remaining; if fewer than k remain, leave them as-is.
  2. Reverse exactly k pointers at a time using the standard prev/cur/next three-pointer swap, then connect the reversed group's new tail to the result of processing the rest of the list.
  3. Recursion cleanly handles the 'connect to the next group' step: reverse_k returns the new head of everything from this group onward. For O(1) space, do it iteratively with a group-previous pointer that stitches each reversed block back in.
Last updated: Jul 1, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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
  • AI Coding 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

  • Elements Occurring More Than n/3 Times in a Sorted Array - Bytedance (medium)
  • Course Schedule Feasibility - Bytedance (hard)
  • Least Frequently Used (LFU) Cache - Bytedance (hard)
  • Reverse a Singly Linked List - Bytedance (medium)
  • Shortest Path in a Grid with Limited Obstacle Removal - Bytedance (medium)