> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/linked-list/reverse-nodes-in-k-group.md).

# Reverse Nodes in k-Group

{% embed url="<https://www.lintcode.com/problem/450/>" %}

## Solution 1 (Java)

```java
/**
 * Definition for ListNode
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */

public class Solution {
    /**
     * @param head: a ListNode
     * @param k: An integer
     * @return: a ListNode
     */
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode cur = dummy;

        while (true) {
            cur = reverseBetween(cur, k);
            if (cur == null) break;
        }
        return dummy.next;
    }


    // modified from Reverse Linked List II
    public ListNode reverseBetween(ListNode head, int k) {
        if (head == null) return head;

        // 1. find the k-th node 
        ListNode kNode = head;
        for (int i = 0; i < k; i++) {
            kNode = kNode.next;
            if (kNode == null) return null;
        }

        ListNode firstNode = head.next;
        ListNode kNextNode = kNode.next;

        // 2. reverse
        ListNode prev = null, next;
        ListNode curr = firstNode;
        while (curr != kNextNode) {
            next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }

        // 3. connect the lists
        head.next = kNode;
        firstNode.next = kNextNode;
        return firstNode;
    }
}
```

### Notes

* We use a similar approach used in [Reverse Linked List II](/algo/linked-list/reverse-linked-list-ii.md). Using the helper function `reverseBetween`, we reverse `k` nodes each time and return the end of of the linked list for each `k` group. If the end node is null, we know that the remaining nodes cannot constitute a `k` group.
* Inside the helper function, note that the `head` is always one node before the starting node that is to be reversed. Then, we keep track of the first node that is to be reversed (`firstNode`) and the node after k-th node (`kNextNode`) because we need to connect the reversed linked list eventually with a start and an end.


---

# 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, and the optional `goal` query parameter:

```
GET https://blog.yushunchen.com/algo/linked-list/reverse-nodes-in-k-group.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
