↓Skip to main content
  1. LeetCode/

LeetCode 25: Reverse Nodes in k-Group

·2 mins· ·
LeetCode Hard Linked-List Recursion
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: hard First Attempt: 2026-11-01

  • Submissions: Accepted (first try)

Approach
#

Count the length first; n // k is the number of groups to reverse, and a final group shorter than k stays as it is.

Use a dummy head (Linked List Template 1), with curr pointing at the previous group’s tail:

  1. Reverse the k nodes starting at curr.next (Template 2). After reversing, the group’s old head becomes its tail.
  2. Point curr.next at the new head and the new tail at the start of the next group.
  3. Move curr to this group’s new tail and continue.

My Attempts
#

Accepted on the first try.

Solution
#

class Solution:
    def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        n = 0
        node = head
        while node:
            n += 1
            node = node.next

        dummy = ListNode(0, head)
        curr = dummy
        for _ in range(n // k):
            group_head = curr.next
            prev, node = None, group_head
            for _ in range(k):
                nxt = node.next
                node.next = prev
                prev = node
                node = nxt
            curr.next = prev            # new head of this group
            group_head.next = node      # new tail links to the next group
            curr = group_head
        return dummy.next

My submission put the reversal in a helper returning (new head, new tail, next group start); same idea.

Complexity
#

Time O(n), extra space O(1).

Mistakes to Avoid
#

  • Do not reverse a final group shorter than k. Counting the length first is simplest; probing k steps ahead per group also works.
  • After reversing, the old head is the tail; keep a reference to it to link the next group.
  • With a dummy head, the first group needs no special handling of the new head.

Interview Explanation
#

I count the length to know how many groups to reverse. With a dummy head and curr at the previous group’s tail, I reverse the next k nodes, link the previous tail to the new head and the group’s old head (now its tail) to the next group, then move curr to that tail. Time O(n), space O(1).

Related

LeetCode 460: LFU Cache
·3 mins
LeetCode Hard Hash-Table Linked-List Design Doubly-Linked-List
LeetCode 460 note: key → node, freq → doubly linked list, plus a min_freq; within the same frequency, LRU decides what to evict
LeetCode 23: Merge k Sorted Lists
·4 mins
LeetCode Hard Linked-List Heap Merge-Sort Divide-and-Conquer
LeetCode 23 note: a heap that holds only k heads, and divide-and-conquer pairwise merging
LeetCode 60: Permutation Sequence
·2 mins
LeetCode Hard Math Combinatorics Recursion
LeetCode 60 note: make k 0-indexed; once the first digit is fixed there are (n-1)! arrangements, so k // (n-1)! picks the first digit among those remaining, then repeat for the next position
LeetCode 143: Reorder List
·2 mins
LeetCode Medium Linked-List Two-Pointers Stack Recursion
LeetCode 143 note: find the middle, reverse the second half, and merge the two alternately, cutting the first half’s tail
LeetCode 24: Swap Nodes in Pairs
·2 mins
LeetCode Medium Linked-List Recursion
LeetCode 24 note: a dummy plus pairs, rewiring prev.next, a.next and b.next together; swap nodes, not values
LeetCode 234: Palindrome Linked List
·2 mins
LeetCode Easy Linked-List Two-Pointers Stack Recursion
LeetCode 234 note: find the middle with fast and slow pointers, reverse the second half, and compare from both ends with O(1) extra space