-
Notifications
You must be signed in to change notification settings - Fork 1k
/
DeleteElementLL.java
172 lines (136 loc) · 4.59 KB
/
DeleteElementLL.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
We will create a LinkedList first and display its elements.
After that we will delete some elements from the LinkedList
* Delete from beginning - In this case we will simply move the head pointer
to the next node. In case if we have only one element in the LL then we
will assign null to head which is the pointer to the first and only node in the LL.
* Delete from end - In this case we will take a pointer and initialize
it to the head pointer and we will move this pointer till end-1 so
that we can access the last node.
* Delete from any position - In this case we will take a pointer variable
and initialize it to the head pointer and we will move this pointer till
position-1 so that we can access the desired position node.
*/
import java.util.Scanner;
public class DeleteElementLL {
//creating the Node class having one data part and one pointer to the next node
class Node{
int data;
Node next;
// Constructor to initialize the data and next pointer
Node(int data){
this.data = data;
this.next = null;
}
}
// creating the head of the list
Node head = null;
public void createList(int newData) {
/* creating a new node with data value.
If list is empty head points to the newly
created node and if list is not empty, we create
temp variable and traverse the list till end
and insert the node at the end */
Node new_Node = new Node(newData);
Node temp = head;
if(head==null) {
head = new_Node;
}
else {
while(temp.next!=null) {
temp = temp.next;
}
temp.next = new_Node;
}
}
public void displayList() {
/* creating a temp variable
traversing and printing the list*/
Node temp = head;
while(temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public void deleteFromBeg() {
/* if list is empty nothing to delete
else head points to the second element in the list */
if(head==null){
System.out.println("List is Empty.");
return;
}
else {
head = head.next;
}
}
public void deleteFromEnd() {
/* if only one element is present in the list
delete it otherwise create a temp variable
and traverse the list till the second
last element and at last temp should
point null to remove last node */
Node temp = head;
if(head.next == null) {
head = null;
}
else{
while(temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
}
public void deleteFromAnyPosition(int pos) {
/* create a temp variable and initialize it to head.
Then traverse the list till position-1 and at
last link the previous node of the node to-be-deleted
to the node after it. */
Node temp = head;
int i=1;
while(i<pos-1) {
temp = temp.next;
i++;
}
Node next = temp.next.next;
temp.next = next;
}
public static void main(String[] args) {
DeleteElementLL list = new DeleteElementLL();
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of elements you want to insert in the List: ");
int n = input.nextInt();
System.out.println("Enter the elements of the List: ");
for(int i=0; i<n; i++) {
list.createList(input.nextInt());
}
System.out.print("Initial List: ");
list.displayList();
list.deleteFromBeg();
System.out.print("List after deletion from the Beginning: ");
list.displayList();
list.deleteFromEnd();
System.out.print("List after deletion from the End: ");
list.displayList();
System.out.println("Enter the position of the element you want to delete: ");
int position = input.nextInt();
input.close();
list.deleteFromAnyPosition(position);
System.out.print("List after deletion from the desired position: " );
list.displayList();
}
}
/*
Enter the number of elements you want to insert in the List:
9
Enter the elements of the List:
3 5 8 2 10 7 21 6 1
Initial List: 3 5 8 2 10 7 21 6 1
List after deletion from the Beginning: 5 8 2 10 7 21 6 1
List after deletion from the End: 5 8 2 10 7 21 6
Enter the position of the element you want to delete:
3
List after deletion from the desired position: 5 8 10 7 21 6
Time Complexity - O(n) Worst case
O(1) Best case
*/