-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathReverse_in_group_of_K.java
84 lines (75 loc) · 2.02 KB
/
Reverse_in_group_of_K.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.util.Scanner;
class Reverse_in_group_of_K {
static class Node {
int data;
Node next;
// Constructor to initialize value
Node(int x) {
data = x;
next = null;
}
}
static Node insert(Node head, int val) {
Node temp = new Node(val);
if (head == null) {
temp.next = head;
return temp;
}
Node curr = head;
while (curr.next != null)
curr = curr.next;
curr.next = temp;
temp.next = null;
return head;
}
static Node reverse(Node head, int k) {
Node curr = head;
Node prevFirst = null;
boolean isFirstPass = true;
while (curr != null) {
Node first = curr;
Node prev = null;
int count = 0;
while (curr != null && count < k) {
Node next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
if (isFirstPass) {
head = prev;
isFirstPass = false;
}
else
prevFirst.next = prev;
prevFirst = first;
}
return head;
}
static void print(Node head) {
Node curr = head;
System.out.println("\n\n");
while (curr != null) {
System.out.print(curr.data + "->");
curr = curr.next;
}
System.out.println("NULL\n\n");
}
public static void main(String[] args) {
Node head = null;
int n, val, k;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter n, k: ");
n = scanner.nextInt();
k = scanner.nextInt();
System.out.println("Enter nodes: ");
for (int i = 0; i < n; i++) {
val = scanner.nextInt();
head = insert(head, val);
}
print(head);
head = reverse(head, k);
print(head);
}
}