Problem: Reverse Nodes in k-Group

Screenshot 2022-08-11 150647.png

Constraints:

The number of nodes in the list is n. 1 <= k <= n <= 5000 0 <= Node.val <= 1000

```class Solution { public ListNode reverseKGroup(ListNode head, int k) { ListNode dummy = new ListNode(-1, head); ListNode endOfPrevGroup = dummy;

    while(true){
        int kCopy = k;
        ListNode kth = endOfPrevGroup;
        while(kth != null && kCopy > 0){
            kth = kth.next;
            kCopy--;
        }
        if(kth == null) break;
        ListNode startOfNextGroup = kth.next;

        ListNode prev = kth.next;
        ListNode curr = endOfPrevGroup.next;
        while(curr != startOfNextGroup){
            ListNode currNext = curr.next;
            curr.next = prev;
            prev = curr;
            curr = currNext;
        }
        ListNode newEndOfPrevGroup = endOfPrevGroup.next;
        endOfPrevGroup.next = kth;
        endOfPrevGroup = newEndOfPrevGroup;
    }
    return dummy.next;
}

} ```

hh.png