Skip to content

Commit c30eaa5

Browse files
committed
Add Solution2.java to problems 0025
1 parent 0fb3858 commit c30eaa5

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)