We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 0fb3858 commit c30eaa5Copy full SHA for c30eaa5
solution/0025.Reverse Nodes in k-Group/Solution2.java
@@ -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
33
+ return res.next;
34
35
+}
0 commit comments