1 public ListNode reverseKGroup(ListNode head, int k) { 2 ListNode p = head, p2 = p; 3 int[]a=new int[k]; 4 5 if (p == null) return head; 6 while (true) { 7 p2=p; 8 for (int i = 0; i < k ; i++) { 9 if (p2 != null) { 10 a[i]=p2.val; 11 p2 = p2.next; 12 } else return head; 13 } 14 p2=p; 15 for (int i = 0; i < k ; i++) { 16 p2.val=a[k-1-i]; 17 p2=p2.next; 18 } 19 if (p2==null)return head; 20 else p=p2; 21 } 22 }
原文:https://www.cnblogs.com/towerbird/p/11574066.html