> For the complete documentation index, see [llms.txt](https://private-26.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://private-26.gitbook.io/notes/coding/hard/25.-reverse-nodes-in-k-group.md).

# 25. Reverse Nodes in k-Group

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 Complexity | Time Complexity |
| ---------------- | --------------- |
| $$\text{O}(1)$$  | $$\text{O}(N)$$ |

## Code

```java
// 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 > 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;
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://private-26.gitbook.io/notes/coding/hard/25.-reverse-nodes-in-k-group.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
