-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReverseNodesInEvenLengthGroups.java
46 lines (39 loc) · 1.27 KB
/
ReverseNodesInEvenLengthGroups.java
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
46
package com.smlnskgmail.jaman.leetcodejava.medium;
import com.smlnskgmail.jaman.leetcodejava.support.ListNode;
import java.util.ArrayList;
import java.util.List;
// https://leetcode.com/problems/reverse-nodes-in-even-length-groups/
public class ReverseNodesInEvenLengthGroups {
private final ListNode input;
public ReverseNodesInEvenLengthGroups(ListNode input) {
this.input = input;
}
public ListNode solution() {
List<ListNode> nodes = new ArrayList<>();
ListNode p = input;
while (p != null) {
nodes.add(p);
p = p.next;
}
int counter = 1;
int length = nodes.size();
int index = 0;
while (length > 0) {
int count = Math.min(counter, length);
if (count % 2 == 0) {
int upperLimit = count + index - 1;
for (int i = index, j = 0; j < count / 2; i++, j++) {
ListNode a = nodes.get(i);
ListNode b = nodes.get(upperLimit - j);
int temp = a.val;
a.val = b.val;
b.val = temp;
}
}
length -= counter;
index += counter;
counter++;
}
return input;
}
}