15% off all plans & roadmaps Use code
See plansLeetCode 25 · Solution & editorial
Given the head of a linked list, reverse the nodes of the list k at a time, and return the modified list.
Reverse Nodes in k-Group is a Linked List Manipulation Patterns problem, in the In-place Reversal subpattern. The pattern covers in-place reversal, merging sorted lists, reordering, and intersection detection
Master in-place linked list reversal including full reversal, partial reversal, and k-group reversal techniques.
The reference solution runs in O(n) time and O(1) space.
Reverse Nodes in k-Group has been reported in interviews at Amazon, Microsoft, Apple, ByteDance, Facebook, Google.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Reverse Nodes in k-Group is a Linked List Manipulation Patterns problem, in the In-place Reversal subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.