/**
 * Reverse Nodes in k-Group
 * 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
 * @author chenshuna
 */
public class reverseKGroup {
    public static ListNode reverselist(ListNode pre,ListNode next){
        ListNode last=pre.next;
        ListNode cur=last.next;
        while(cur!=next){
            last.next=cur.next;
            cur.next=pre.next;
            pre.next=cur;
            cur=last.next;
        }
        return last;
    }
    
    public static ListNode reverseKGroup(ListNode head, int k) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode newhead=new ListNode(0);
        newhead.next=head;
        ListNode pre=newhead;
        ListNode cur=head;
        int count=0;
        while(cur!=null){
            count++;
            ListNode next=cur.next;
            if(count==k){
                pre=reverselist(pre,next);
                count=0;
            }
            cur=next;
        }
        return newhead.next;
    }

    public static void main(String[] args) {
		// TODO Auto-generated method stub
        ListNode res = new ListNode(1);
        res.next = new ListNode(2);
        res.next.next =  new ListNode(3);
        res.next.next.next = new ListNode(4);
        res.next.next.next.next = new ListNode(5);
        ListNode display = reverseKGroup(res, 2);
        while(display != null){
            System.out.print(display.val);
            display = display.next;
        }
       
	}

}

@@ -640,17 +640,19 @@
 ode last
-=
+ = 
 pre.next
@@ -669,25 +669,27 @@
 ListNode cur
-=
+ = 
 last.next;%0A 
@@ -704,18 +704,20 @@
 hile(cur
+ 
 !=
+ 
 next)%7B%0A 
@@ -732,25 +732,27 @@
    last.next
-=
+ = 
 cur.next;%0A  
@@ -769,17 +769,19 @@
 cur.next
-=
+ = 
 pre.next
@@ -802,17 +802,19 @@
 pre.next
-=
+ = 
 cur;%0A   
@@ -825,17 +825,19 @@
      cur
-=
+ = 
 last.nex
@@ -965,16 +965,20 @@
 head
+ 
 ==
+ 
 null
+ 
 %7C%7C
+ 
 head
@@ -982,18 +982,20 @@
 ead.next
+ 
 ==
+ 
 null)%7B%0A 
@@ -1052,17 +1052,19 @@
  newhead
-=
+ = 
 new List
@@ -1092,17 +1092,19 @@
 ead.next
-=
+ = 
 head;%0A  
@@ -1121,17 +1121,19 @@
 Node pre
-=
+ = 
 newhead;
@@ -1153,17 +1153,19 @@
 Node cur
-=
+ = 
 head;%0A  
@@ -1175,25 +1175,27 @@
    int count
-=
+ = 
 0;%0A        w
@@ -1202,18 +1202,20 @@
 hile(cur
+ 
 !=
+ 
 null)%7B%0A 
@@ -1259,17 +1259,19 @@
 ode next
-=
+ = 
 cur.next
@@ -1292,18 +1292,20 @@
 if(count
+ 
 ==
+ 
 k)%7B%0A    
@@ -1319,17 +1319,19 @@
      pre
-=
+ = 
 reversel
@@ -1366,17 +1366,19 @@
    count
-=
+ = 
 0;%0A     
@@ -1401,17 +1401,19 @@
      cur
-=
+ = 
 next;%0A  
@@ -1505,45 +1505,8 @@
 ) %7B%0A
-%09%09// TODO Auto-generated method stub%0A
     

fix bug