forked from sushovankarmakar/GeeksforGeeks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DLinkedListDeletion.java
87 lines (70 loc) · 1.7 KB
/
DLinkedListDeletion.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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class DLinkedListDeletion {
Node head;
static class Node{
int data;
Node prev, next;
Node(int d){
data = d;
prev = next = null;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
int n = Integer.parseInt(br.readLine().trim());
String ip[] = br.readLine().trim().split("\\s+");
int x = Integer.parseInt(br.readLine().trim());
Node head = null;
for (int i = 0; i < n; i++) {
int data = Integer.parseInt(ip[i]);
head = insert(head, data);
}
printDll(head);
head = delete(head, x);
printDll(head);
}
}
static Node insert(Node head, int data) {
Node n = new Node(data); // creation of a new node
Node temp = head;
if(head==null) {
head = n;
}
else {
while(temp.next !=null) {
temp = temp.next;
}
temp.next = n;
n.prev = temp;
}
return head;
}
static void printDll(Node head) {
StringBuffer sb = new StringBuffer();
Node temp = head;
while(temp != null) {
sb.append(temp.data+" ");
temp = temp.next;
}
System.out.println(sb);
sb.setLength(0);
}
static Node delete(Node head, int x) {
Node temp = head;
while( (--x) !=0 ) {
if (temp.next != null) temp = temp.next;
}
if(temp.prev != null) temp.prev.next = temp.next;
else { // special case only if first node is getting deleted
temp.next.prev = null;
head = temp.next;
}
if(temp.next != null) temp.next.prev = temp.prev;
temp.prev = temp.next = null;
return head;
}
}