forked from Shreyasheeetal20/HACKTOBERFEST22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete_every_n_nodes.java
98 lines (76 loc) · 2.49 KB
/
delete_every_n_nodes.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.io.*;
import java.util.*;
class LinkedListNode<T> {
T data;
LinkedListNode<T> next;
public LinkedListNode(T data) {
this.data = data;
}
}
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static LinkedListNode<Integer> takeInput() throws IOException {
LinkedListNode<Integer> head = null, tail = null;
String[] datas = br.readLine().trim().split("\\s");
int i = 0;
while(i < datas.length && !datas[i].equals("-1")) {
int data = Integer.parseInt(datas[i]);
LinkedListNode<Integer> newNode = new LinkedListNode<Integer>(data);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
i += 1;
}
return head;
}
public static void print(LinkedListNode<Integer> head){
while(head != null) {
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
public static LinkedListNode<Integer> skipMdeleteN(LinkedListNode<Integer> head, int M, int N) {
if (N == 0 || head == null)
return head;
if (M == 0)
return null;
int cm= 0, cn= 0;
LinkedListNode<Integer> temp= head, hc= head;
while (temp != null)
{
cm++;
if (cm < M)
hc= hc.next;
else
cn++;
if (cn > 0 && cn <= N && temp.next == null)
hc.next= null;
else if (cn == N+1){
hc.next= temp.next;
hc= temp.next;
cn= 0;
cm= 0;
}
temp= temp.next;
}
return head;
}
public static void main(String[] args) throws NumberFormatException, IOException {
int t = Integer.parseInt(br.readLine().trim());
while (t > 0) {
LinkedListNode<Integer> head = takeInput();
String[] m_n = br.readLine().trim().split("\\s");
int m = Integer.parseInt(m_n[0]);
int n = Integer.parseInt(m_n[1]);
LinkedListNode<Integer> newHead = skipMdeleteN(head, m, n);
print(newHead);
t -= 1;
}
}
}