package leetcode;

/**
 * https://leetcode.com/problems/reverse-nodes-in-k-group/
 */
public class Problem25 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }

        @Override
        public String toString() {
            return "" + val;
        }
    }

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNodeRef ref = new ListNodeRef();
        ListNode n = reverse(head, k, 1, ref);
        n.next = ref.second;
        return ref.first;
    }

    private static class ListNodeRef {
        private ListNode first;
        private ListNode second;
    }

    private static ListNode reverse(ListNode node, int k, int i, ListNodeRef ref) {
        if (i == k) {
            ref.first = node;
            ref.second = node.next;
            return node;
        }
        ListNode n = reverse(node.next, k, i + 1, ref);
        n.next = node;
        node.next = null;
        return node;
    }

    private static void print(ListNode node) {
        for (ListNode n = node; n != null; n = n.next) {
            System.out.print(n + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Problem25 prob = new Problem25();

        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        print(prob.reverseKGroup(head, 2)); // 2 1 4 3 5

        head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        print(prob.reverseKGroup(head, 3)); // 3 2 1 4 5 
    }
}

@@ -241,58 +241,148 @@
   %7D%0A
-%0A
     
+%7D%0A%0A
     
-@Override%0A        public String toString(
+public ListNode reverseKGroup(ListNode head, int k) %7B%0A        int size = 0;%0A        for (ListNode n = head; n != null; n = n.next
 ) %7B%0A
@@ -397,23 +397,14 @@
     
-return %22%22 + val
+size++
 ;%0A  
@@ -419,68 +419,78 @@
     
-%7D%0A%0A    public ListNode reverseKGroup(ListNode head, int k) %7B
+    if (head == null %7C%7C size %3C k) %7B%0A            return head;%0A        %7D
 %0A   
@@ -494,24 +494,16 @@
         
-ListNode
 Ref ref 
@@ -508,24 +508,16 @@
 f = new 
-ListNode
 Ref();%0A 
@@ -555,16 +555,32 @@
 d, k, 1,
+ (size / k) * k,
  ref);%0A 
@@ -665,24 +665,16 @@
 c class 
-ListNode
 Ref %7B%0A  
@@ -732,24 +732,51 @@
 ode second;%0A
+        private int count;%0A
     %7D%0A%0A    p
@@ -835,24 +835,23 @@
  int i, 
-ListNode
+int j, 
 Ref ref)
@@ -870,17 +870,17 @@
 f (i == 
-k
+j
 ) %7B%0A    
@@ -941,16 +941,41 @@
 e.next;%0A
+            ref.count++;%0A
         
@@ -1050,16 +1050,19 @@
 , i + 1,
+ j,
  ref);%0A 
@@ -1072,31 +1072,39 @@
     
-n.next = node;%0A
+if (ref.count %3C k) %7B%0A  
         
 node
@@ -1103,40 +1103,18 @@
     
-node
+  n
 .next =
- null;%0A        return
  nod
@@ -1124,407 +1124,142 @@
     
-%7D%0A%0A
+ 
     
-private static void print(ListNode node) %7B%0A        for (ListNode n = node; n != null; n = n.next) %7B%0A            System.out.print(n + %22 %22);%0A        %7D%0A        System.out.println();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem25 prob = new Problem25();%0A%0A        ListNode head = new ListNode(1);%0A        head.next = new ListNode(2);%0A        head.next.next = new ListNode(3)
+   node.next = null;%0A            ref.count++;%0A        %7D else %7B%0A            node.next = ref.first;%0A            n.next = ref.second
 ;%0A  
@@ -1267,425 +1267,124 @@
     
- 
-head.next.next.next = new ListNode(4);%0A        head.next.next.next.next = new ListNode(5);%0A        print(prob.reverseKGroup(head, 2)); // 2 1 4 3 5%0A%0A        head = new ListNode(1);%0A        head.next = new ListNode(2);%0A        head.next.next = new ListNode(3);%0A        head.next.next.next = new ListNode(4);%0A        head.next.next.next.next = new ListNode(5);%0A        print(prob.reverseKGroup(head, 3)); // 3 2 1 4 5 
+     ref.first = node;%0A            ref.second = node.next;%0A            ref.count = 1;%0A        %7D%0A        return node;
 %0A   

Solve problem 25