# O(1) Space: Very intuitive easy to understand Java solution

• If you breakdown this problem in multiple pieces it is very easy.
What if someone provides you a function that returns back three pieces of information for each K chunks of list.

2. Tail of k chunk of link list
3. The immediate node that comes after this chunk.
Example:
1->2->3->4 and k = 3
the function provides below and transforms link list to 3->2->1
newTail = 1
nextToDealNode = 4

Now all you need to do is point the newTail with the nextToDealNode.
When there is no more nextToDealNode, you are down.

Writing such function is not that difficult task.

``````public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {

ListNode nTail = r.tail;

while(r.nextToDeal != null){
r = reverseKList(r.nextToDeal, k);
nTail = r.tail;
}

}

private Result reverseKList(ListNode head, int k){
if(! okToRev){
}

ListNode nTail = head; // preserve tail
ListNode n = head, temp = null, prev = null;
while(k > 0){
temp = n.next;
if(prev !=null){
n.next = prev;
}
prev = n;
n = temp;
k--;
}

// Next to deal with node is temp
nTail.next = null;
return new Result(prev,nTail,temp);
}

/*
true: If the list is big enough for k
*/
private boolean canReverse(ListNode head, int k){
while(k > 0 && head !=null){
k--;
}

// If we hit tail, we will not reverse the list
return (head == null && k!=0) ? false : true;

}

private static class Result{