### LeetCode 25：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.

k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

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`

Note:

• Only constant extra memory is allowed.
• You may not alter the values in the list’s nodes, only nodes itself may be changed.

`package com.lihuia.leetcode25;import java.util.Stack;/** * Copyright (C), lihuia.com * FileName: Solution * Author:   lihui * Date:     2018/7/22 */public class Solution {        public ListNode reverseKGroup(ListNode head, int k) {        ListNode current = head;        Stack<ListNode> stack = new Stack<ListNode>();                for (int i = 0; i < k; i++) {            if (head != null) {                stack.push(head);            } else {                //不满K个                return current;            }            head = head.next;        }                ListNode first = stack.pop();        ListNode reverse = first;        for (int i = 0; i < k - 1; i++) {            first.next = stack.pop();            first = first.next;        }        //后面每组倒序后新生成的链表，会接在当前该组的后方        first.next = reverseKGroup(head, k);        return reverse;    }}`

`package com.lihuia.leetcode25;import javax.annotation.Resource;import org.testng.annotations.BeforeClass;import org.testng.annotations.Test;/** * Copyright (C), lihuia.com * FileName: SolutionTest * Author:   lihui * Date:     2018/7/22 */public class SolutionTest {        @Resource    private ListNode head;    private ListNode first;    private Solution solution;    private int maxLen = 10;    private int k = 4;        @BeforeClass    public void beforeClass() {        head = new ListNode(0);        first = head;        for (int i = 1; i < maxLen; i++) {            head.next = new ListNode(i);            head = head.next;        }        solution = new Solution();    }        @Test    public void testReverseKGroup() {        ListNode reverseListNode = solution.reverseKGroup(first, k);        while (reverseListNode != null) {            System.out.print(reverseListNode.val + " ");            reverseListNode = reverseListNode.next;        }    }}`

first和reverse都指向3这个节点，接着reverse指向链表3->2->1->0，first指向了0这个节点

first和reverse都指向7这个节点，接着reverse指向链表7->6->5->4，first指向了4这个节点