-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListReverseKGroups.java
46 lines (40 loc) · 1.57 KB
/
LinkedListReverseKGroups.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
// Consider a single linked list and an integer k. Write a snippet of code to reverse the list's nodes into k groups.
//e.g 7->4->3->1->8->2->9->0 should become 3->4->7->2->8->1->0->9
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedListReverseKGroups {
public static void main(String[] args) {
final Scanner scanner = new Scanner(System.in);
final LinkedList<Integer> list = new LinkedList<>();
final LinkedList<Integer> result = new LinkedList<>();
try (scanner) {
System.out.println("Enter k (groups): ");
int k = scanner.nextInt();
System.out.println("Enter the elements: ");
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
list.add(scanner.nextInt());
} else {
if (scanner.next().equals("done")) {
break;
}
}
}
reverse(list, result, k);
}
System.out.println("Result: ");
result.forEach(System.out::println);
}
private static void reverse(final LinkedList<Integer> list, final LinkedList<Integer> result, final int k) {
for (int i = 0; i < list.size(); i = i + k) {
int leftIndex = i;
int rightIndex = i + 2;
for (; rightIndex >= leftIndex; rightIndex--) {
if (rightIndex < list.size()) {
int integer = list.get(rightIndex);
result.add(integer);
}
}
}
}
}