HardLinked ListRecursionUpdated Sep 2026

25. Reverse Nodes in k-Group

Asked at Walmart, TikTok

Difficulty

Hard

Time

O(n)

Space

O(1)

Pattern

Linked List

Companies

2

Problem

You are given the head of a singly linked list and a positive integer k. Reverse the list in blocks of k nodes at a time, then return the head of the new list.

A block means k nodes that are next to each other. You reverse each block internally, but the blocks themselves stay in the same order. If the last block has fewer than k nodes, leave those leftover nodes exactly as they are — do not reverse them.

You must relink the nodes, not change the values inside them. Try to use only O(1) extra memory (a few pointer variables), not an array of all nodes.

Reverse each block of k, keep the rest

input12345nullSplit the list into groups of k = 2; the last group has only 1node.output21435nullEach full block is flipped in place; the leftover node keepsits position.
With k = 2 the list splits into [1,2], [3,4] and the leftover [5]; each full block flips, the leftover does not.

Constraints

  • ●The number of nodes is n.
  • ●1 <= k <= n <= 5000
  • ●0 <= Node.val <= 1000

Examples

Example 1
Inputhead = [1,2,3,4,5], k = 2
Output[2,1,4,3,5]
WhyThe list splits into blocks [1,2], [3,4] and the leftover [5]. Reversing the first two blocks gives [2,1] and [4,3]. The single leftover node 5 stays in place, so the result is 2 → 1 → 4 → 3 → 5.
Example 2
Inputhead = [1,2,3,4,5], k = 3
Output[3,2,1,4,5]
WhyThe first block is [1,2,3] and it reverses to [3,2,1]. Only 2 nodes remain, which is less than k = 3, so [4,5] is left untouched. The result is 3 → 2 → 1 → 4 → 5.

Asked At

2 companies

How to Think About It

1

Brute force: convert to array, reverse every k elements, and rebuild the list.

2

Count nodes first to determine how many full k-groups exist.

3

Reverse each k-group using iterative reversal with three pointers.

4

Connect the tail of the previous group to the head of the reversed group.

5

Optimal: count total nodes, then iteratively reverse exactly k nodes per group, linking groups together.

Optimal Approach

First count the total number of nodes to determine how many complete k-groups exist. Use a dummy head node to handle the case where the first group is reversed. For each group, reverse k nodes iteratively by maintaining prev, curr, and next pointers. After reversing a group, connect the previous group tail to the new head, and advance the tail pointer. Repeat for each full group. Time complexity is O(n) with O(1) extra space.

Count, then reverse k nodes at a time

before12345nullcurrnextStart a group at node 1. Walk k = 2 nodes, flipping each nextpointer backwards.after group 121345nullgroup tailcurrGroup 1 is now 2 → 1. Link the dummy head to 2 and the oldstart 1 to the next group.after group 221435nullgroup tailcurrGroup 2 becomes 4 → 3 and is linked after 1. Only 1 noderemains, less than k, so we stop.groups = total // keach group: flip k next pointersleftover nodes: leave as is
We count the nodes first, so we know how many full groups exist and can stop before a short tail.
ComplexityTime O(n)Space O(1)We walk the list once to count nodes, then each node is touched a constant number of times during reversal, and only a fixed set of pointers is stored.

What Trips People Up in Real Interviews

1

Clarify that only complete k-groups are reversed and leftover nodes remain as-is.

2

Mention the need to count total nodes first to know how many groups to reverse.

3

Explain the iterative reversal technique using prev, curr, and next pointers.

4

Discuss using a dummy head to simplify edge case handling for the first group.

5

Address the constant space requirement ruling out recursion stack as a solution.

Solution Code

O(n) time
class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        def count_nodes(node):
            count = 0
            while node:
                count += 1
                node = node.next
            return count
        
        total = count_nodes(head)
        dummy = ListNode(0, head)
        prev_group_tail = dummy
        curr = head
        
        while total >= k:
            prev = None
            group_start = curr
            for _ in range(k):
                next_node = curr.next
                curr.next = prev
                prev = curr
                curr = next_node
            
            prev_group_tail.next = prev
            group_start.next = curr
            prev_group_tail = group_start
            total -= k
        
        return dummy.next

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Reverse Nodes in k-Group problem?

Given the head of a linked list, reverse the nodes k at a time and return the modified list. If the number of nodes is not a multiple of k, the remaining nodes at the end should stay in their original order. Only constant extra memory may be used.

How do you solve Reverse Nodes in k-Group?

The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Reverse Nodes in k-Group?

Reverse Nodes in k-Group is asked at Walmart, TikTok. It is a hard difficulty problem.

What are common mistakes on Reverse Nodes in k-Group?
  • Clarify that only complete k-groups are reversed and leftover nodes remain as-is.
  • Mention the need to count total nodes first to know how many groups to reverse.
  • Explain the iterative reversal technique using prev, curr, and next pointers.
  • Discuss using a dummy head to simplify edge case handling for the first group.
  • Address the constant space requirement ruling out recursion stack as a solution.