import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @param k int整型 * @return ListNode类 */ public ListNode reverseKGroup (ListNode head, int k) { // write code here if(head == null){ return head; } ListNode end = head,begin = head; for(int i = 0 ; i < k; i++){ if(end == null){ return head;} end = end.next; } ListNode newHead = reverseAToB(begin,end); begin.next = reverseKGroup(end,k); return newHead; } public ListNode reverseAToB(ListNode a,ListNode b){ ListNode pre = null,cur = a,nxt = a; while(cur != b){ nxt = cur.next; cur.next = pre; pre = cur; cur = nxt; } return pre; } }