Skip to content
AI360Xpert
Beta
Difficulty: HardLinked List

Reverse Nodes in k-Group

Problem in Plain English

Given the head of a linked list and an integer k, reverse the nodes k at a time, leaving fewer than k trailing nodes as they are, using only pointer changes.

Problem Statement

Given the head of a linked list, reverse its nodes k at a time and return the modified list.

If fewer than k nodes remain at the end, leave them as they are. Only node links may change, never node values.

Constraints

  • The number of nodes in the list is nn.
  • 1≤k≤n≤50001 \le k \le n \le 5000.
  • 0≤Node.val≤10000 \le \mathrm{Node.val} \le 1000.
  • Only change links between the existing nodes; do not change their values.

Examples

Example 1

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

Reverse the complete pairs of original nodes [1,2] and [3,4]. The final node 5 cannot form a pair, so its next link remains null.

Example 2

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

Reverse the first complete group of three. The trailing nodes 4 and 5 form a short group: keep 4 -> 5 -> null unchanged and connect the new tail 1 to node 4.

Example 3

Input
head = [7], k = 1
Output
[7]

A one-node group is already reversed. Return the same node with its value and null next link unchanged.

Example 4

Input
head = [1,2,3,4], k = 4
Output
[4,3,2,1]

The entire list is one complete group. The original head 1 becomes the last node and must point to null.

Intuition

A full group can be reversed by changing its links, but its original first node becomes its new tail. In Example 1, reversing nodes 1 and 2 creates 2 -> 1; node 1 must then connect to the next reversed group, 4 -> 3. Keep the boundary after each group before rewiring anything. If the suffix has fewer than k nodes, keep every link in that suffix unchanged. Node identity matters: swapping values would produce the same numbers but violate the requirement.

Approaches

Recursive Groups

Solution Details

Reveal Recursive Groups: intuition, complexity, and code

Hints

Hint 1
A short final group must keep all its original links. How can you prove the next group has k nodes before changing anything?
Hint 2
Reversing k nodes makes the original group head its new tail. Save both that node and the pointer to the next group.
Hint 3
Use either a recursive suffix answer or a dummy node and previous-group tail to reconnect the two boundaries after each reversal.

Edge Cases

  • When k is 1, return the same head and keep every node and link unchanged.
  • When k equals n, reverse the entire list and ensure the original head now points to null.
  • If n is a multiple of k, every group reverses; otherwise the final n modulo k nodes retain all their original next links.
  • Equal values can hide a wrong answer: even [8,8] with k = 2 must reverse the two node identities.
  • At n = 5000 and k = 2, recursion has about 2500 group frames. The Python wrapper reserves capacity; prefer the iterative version when constant space is required.

Common Mistakes and Interview Tips

  • Reversing until null without first checking group size incorrectly changes a short trailing group.
  • Redirecting a next link before saving it loses the unreversed suffix.
  • Treating the old group head as the new head: it is the new tail and must connect to the next suffix.
  • Advancing prevGroupTail to the new group head instead of the new tail splices the next group into the previous group.
  • Swapping node values or allocating replacement output nodes hides an identity violation even when the displayed values match.
  • Claiming constant space for recursion ignores the waiting group frames; without a k = 1 early return, even unchanged single-node groups unnecessarily deepen recursion.

Key Takeaway

For group operations on a linked list, first prove that the group is complete, then preserve its outside boundary before rewiring. The old head becomes the reversed group tail, so reconnect both ends and advance from that tail. A dummy node makes the first group follow the same rules as later groups; recursion instead stores the pending tail connections on its stack.