LeetCode 25. K flip list in groups

 
When execution: 1 ms, beat the 99.35% of all users to submit in Java
Memory consumption: 38.9 MB, defeated 74.50% of all users to submit in Java
 
Sorry, I was an exchange of value, with the array saved, did not expect a -3- ......
 
 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     }

 

Guess you like

Origin www.cnblogs.com/towerbird/p/11574066.html