## [leetcode]算法题目 - 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

 1 /**
2  * Definition for singly-linked list.
3  * public class ListNode {
4  *     int val;
5  *     ListNode next;
6  *     ListNode(int x) {
7  *         val = x;
8  *         next = null;
9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode reverseKGroup(ListNode head, int k) {
14         int len = 0;
17             len++;
19         }
21
22         int reverseTimes = (len / k);
23
24         ListNode myhead = new ListNode(0);
28         ListNode prev = null;
30         ListNode rtail = curr.next;
31
32         for(int j=0;j<reverseTimes;j++){
34             rtail = curr.next;
35             curr = curr.next;
36             next = curr;
37             for (int i = 0; i < k; i++) {
38                 next = next.next;
39                 curr.next = prev;
40                 prev = curr;
41                 curr = next;
42             }
50 }