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) {
        // TODO
        return null;
    }

    private static class ListNodeRef {
        private ListNode ref;
    }

    private static void reverse(ListNode node, int k, int i, ListNodeRef ref) {

    }

    private static void print(ListNode node) {
        for (ListNode n = node; n != null; n = node.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);
        System.out.println(prob.reverseKGroup(head, 2));

        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);
        System.out.println(prob.reverseKGroup(head, 3));
    }
}

@@ -407,35 +407,145 @@
     
-// TODO%0A        return null
+ListNodeRef ref = new ListNodeRef();%0A        ListNode n = reverse(head, k, 1, ref);%0A        n.next = ref.second;%0A        return ref.first
 ;%0A  
@@ -613,19 +613,54 @@
 istNode 
-ref
+first;%0A        private ListNode second
 ;%0A    %7D%0A
@@ -679,20 +679,24 @@
  static 
-void
+ListNode
  reverse
@@ -744,16 +744,264 @@
  ref) %7B%0A
+        if (i == k) %7B%0A            ref.first = node;%0A            ref.second = node.next;%0A            return node;%0A        %7D%0A        ListNode n = reverse(node.next, k, i + 1, ref);%0A        n.next = node;%0A        node.next = null;%0A        return node;
 %0A    %7D%0A%0A
@@ -1091,27 +1091,24 @@
  null; n = n
-ode
 .next) %7B%0A   
@@ -1134,16 +1134,22 @@
 .print(n
+ + %22 %22
 );%0A     
@@ -1505,34 +1505,21 @@
         
-System.out.
 print
-ln
 (prob.re
@@ -1540,16 +1540,29 @@
 ad, 2));
+ // 2 1 4 3 5
 %0A%0A      
@@ -1773,34 +1773,21 @@
         
-System.out.
 print
-ln
 (prob.re
@@ -1808,16 +1808,30 @@
 ad, 3));
+ // 3 2 1 4 5 
 %0A    %7D%0A%7D

Update problem 25