// Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

// If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

// You may not alter the values in the nodes, only nodes itself may be changed.

// Only constant memory is allowed.

// For example,
// Given this linked list: 1->2->3->4->5

// For k = 2, you should return: 2->1->4->3->5

// For k = 3, you should return: 3->2->1->4->5

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(k <= 1 || head == null || head.next == null) return head;

        ListNode fake = new ListNode(0);
        fake.next = head;

        ListNode runner = fake;

        while(runner != null){
        	ListNode tmpRunner = runner;
        	int counter = 0;
        	while(counter < k && tmpRunner != null){
        		counter++;
        		tmpRunner = tmpRunner.next;
        	}
        	if(counter != k || tmpRunner == null) break;

        	ListNode start = runner.next;

        	runner.next = reverse(start, tmpRunner);

        	for(int i = 0; i < k; i++){
        		runner = runner.next;
        	}
        }

        return fake.next;
    }

    public ListNode reverse(ListNode start, ListNode end){
    	ListNode finalEnd = end.next;
    	ListNode pre = start;
    	ListNode curr = start.next;

    	while(curr != finalEnd){
    		ListNode next = curr.next;
    		curr.next = pre;
    		pre = curr;
    		curr = next;
    	}
    	start.next = finalEnd;
    	return end;
    }
}
@@ -1741,8 +1741,1326 @@
 %0A    %7D%0A%7D
+%0A%0A%0A%0A/**%0A * Definition for singly-linked list.%0A * public class ListNode %7B%0A *     int val;%0A *     ListNode next;%0A *     ListNode(int x) %7B%0A *         val = x;%0A *         next = null;%0A *     %7D%0A * %7D%0A */%0Apublic class Solution %7B%0A    public ListNode reverseKGroup(ListNode head, int k) %7B%0A        if(head == null %7C%7C k %3C= 1)%7B%0A            return head;%0A        %7D%0A        %0A        ListNode dummy = new ListNode(1);%0A        dummy.next = head;%0A        ListNode runner = dummy;%0A        %0A        while(runner != null && runner.next != null)%7B%0A            runner = reverse(runner, k);%0A        %7D%0A        %0A        return dummy.next;%0A    %7D%0A    %0A    // should return the next previous node.%0A    public ListNode reverse(ListNode previous, int k)%7B%0A        ListNode runner = previous;%0A        int counter = 0;%0A        while(counter %3C= k && runner != null)%7B%0A            runner = runner.next;%0A            counter++;%0A        %7D%0A        %0A        if(counter %3C= k)%7B%0A            // sub list is shorter than k.%0A            return null;%0A        %7D%0A        %0A        ListNode current = previous.next;%0A        ListNode next;%0A        while(k %3E 1)%7B%0A            next = current.next;%0A            current.next = next.next;%0A            next.next = previous.next;%0A            previous.next = next;%0A            k--;%0A        %7D%0A        %0A        return current;%0A    %7D%0A%7D

update reverse nodes in k group solution