Skip to main content
Back to problems
Leetcode
Medium
Linked Lists
Recursion
Amazon
Microsoft
Google
Reverse Nodes in K Group

Reverse the nodes of a singly linked list in groups of size kk.

Acceptance 100%
Problem Statement

Given the head of a singly linked list and an integer kk, reverse the nodes of the list kk at a time.

  • If the number of nodes in the current group is exactly kk, reverse that group in place.
  • If the remaining nodes at the end contain fewer than kk nodes, leave them in their original order.
  • You must rearrange the node links; do not create a new list from the values.

Return the head of the modified list.

Input Format

  • A singly linked list represented by its head node.
  • An integer kk with k1k \ge 1.

Output Format

Return the head node of the linked list after reversing every consecutive block of kk nodes.

Constraints

  • The list length is at least $0$.
  • 1k1 \le k.
  • Each node appears in exactly one position in the final list.
  • The solution should work in-place with O(1)O(1) extra list storage, aside from recursion stack if a recursive approach is used.
Examples
Sample cases returned by the problem API.

Example 1

Input

head = [1,2,3,4,5], k = 2

Output

[2,1,4,3,5]

Explanation

The list is split into groups of 2: [1,2], [3,4], and [5]. The first two groups are reversed, while the last group has fewer than 2 nodes and stays unchanged.

Example 2

Input

head = [1,2,3,4,5], k = 3

Output

[3,2,1,4,5]

Explanation

The first group [1,2,3] is reversed. The remaining nodes [4,5] are fewer than 3, so they remain in place.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.