File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
solution/0025.Reverse Nodes in k-Group Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+
11
+ public ListNode reverseKGroup (ListNode head , int k ) {
12
+ if (k == 1 ) return head ;
13
+ ListNode res = new ListNode (0 );
14
+ ListNode slow = head , fast = head , index = res ;
15
+ int i = 1 ;
16
+ while (fast != null ) {
17
+ ListNode temp = fast .next ;
18
+ if (i ++ % k == 0 ) {
19
+ fast .next = null ;
20
+ ListNode zou = slow ;
21
+ while (slow != null ) {
22
+ ListNode itme = slow .next ;
23
+ slow .next = index .next ;
24
+ index .next = slow ;
25
+ slow = itme ;
26
+ }
27
+ index = zou ;
28
+ slow = temp ;
29
+ }
30
+ fast = temp ;
31
+ }
32
+ index .next = slow ;
33
+ return res .next ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments