logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
Table Of Contents
25. Reverse Nodes in k-Group26. Remove Duplicates from Sorted Array27. Remove Element42. Trapping Rain Water51. N-Queens55. Jump Game80. Remove Duplicates from Sorted Array II88. Merge Sorted Array121. Best Time to Buy and Sell Stock122. Best Time to Buy and Sell Stock II189. Rotate Array274. H-Index
Code Scratch/leetcode/hard

25. Reverse Nodes in k-Group

2 min read
On this pageComplexity
ComplexityCode

The goal is to reverse the nodes of a linked list in groups of size k. If the remaining number of nodes is less than k, they should stay in their original order.

We do this by identifying chunks of size k, reversing them, and reconnecting the reversed parts back into the main list.

For each group of size k:

  • Find the groupEnd.
  • Temporarily break the connection to the rest of the list.
  • Reverse the group.
  • Reconnect the reversed group to the list.
  • Update prevGroupTail and groupStart to process the next group.

# Complexity

Space ComplexityTime Complexity
O(1)\text{O}(1)O(1)O(N)\text{O}(N)O(N)

# Code

// Helper function to reverse a linked list starting from 'node'
void reverse(ListNode node) {
    ListNode previous = null;
    while (node != null) {
        ListNode nextNode = node.next;
        node.next = previous;
        previous = node;
        node = nextNode;
    }
}
 
public ListNode reverseKGroup(ListNode head, int k) {
    // No need to process if k <= 1 or list is empty
    if (k <= 1 || head == null) return head;
 
    // Dummy node simplifies edge cases (e.g. reversing at head)
    ListNode dummyNode = new ListNode(-1, head);
    ListNode prevGroupTail = dummyNode;
    ListNode groupStart = head;
 
    while (groupStart != null) {
        ListNode groupEnd = groupStart;
        int count = k;
 
        // Find the end node of the current group
        while (count &gt; 1 && groupEnd != null) {
            groupEnd = groupEnd.next;
            count--;
        }
 
        // If the group is smaller than k, no reversal needed
        if (groupEnd == null) break;
 
        // Detach the group and save the remaining list
        ListNode nextGroupStart = groupEnd.next;
        groupEnd.next = null;
 
        // Reverse the current group
        reverse(groupStart);
 
        // Connect the previous group to the new head of this group
        prevGroupTail.next = groupEnd;
 
        // Connect the tail of the reversed group to the next part
        groupStart.next = nextGroupStart;
 
        // Update pointers for next iteration
        prevGroupTail = groupStart;
        groupStart = nextGroupStart;
    }
 
    return dummyNode.next;
}
 
←Previous2965. Find Missing and Repeated ValuesNext→26. Remove Duplicates from Sorted Array

On this page

ComplexityCode