-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0025.java
More file actions
45 lines (39 loc) · 1.37 KB
/
_0025.java
File metadata and controls
45 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.github.aditya;
public class _0025 {
// 1 ms, faster than 55.71%, memory 45.1 MB, less than 77.63%
// Time Complexity = O(n/k) * O(k) ~ O(n), Space Complexity - O(1)
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 1) return head;
ListNode tempNode = new ListNode();
tempNode.next = head;
ListNode prevNode = tempNode, currNode = tempNode, nextNode;
int count = 0;
while (currNode.next != null) {
currNode = currNode.next;
count++;
}
while (count >= k) {
currNode = prevNode.next;
nextNode = currNode.next;
for (int i = 1; i < k; i++) {
currNode.next = nextNode.next;
nextNode.next = prevNode.next;
prevNode.next = nextNode;
nextNode = currNode.next;
}
prevNode = currNode;
count -= k;
}
return tempNode.next;
}
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}