Idea

Set runner for k distance

Reverse k nodes between current ~ runner
public ListNode reverseKGroup(ListNode head, int k) {
if(k == 1) {
return head;
}ListNode dummy = new ListNode(1); dummy.next = head; ListNode current = dummy; ListNode runner = dummy; while(runner != null) { // set distance for (int i = 0; i <= k; i++) { if (runner == null) { return dummy.next; } runner = runner.next; } // reverse ListNode h = current, prev = null, next; current = current.next; while(current != runner) { next = current.next; current.next = prev; prev = current; current = next; } // adjust pointers current = h.next; current.next = runner; runner = h.next; h.next = prev; } return dummy.next;
}