-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleLinkedList.java
290 lines (252 loc) · 8.76 KB
/
SingleLinkedList.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// the single linked list node
class Node{
int data;
Node next;
Node(int x){
this.data=x;
this.next=null;
}
}
public class SingleLinkedList {
// head and tail of linked list
// we are maintaining a tail pointer too, as it helps us in inserting at the last of linked list very //easy
Node head;
Node tail;
// function to add a new node in linked list
// Time Complexity - O(1) , Space Complexity - O(1)
public void addNode(int x){
// create a new node with data x, Time Complexity - O(1) , Space Complexity - O(1)
Node newNode = new Node(x);
// if linked list is empty
if(head==null){
head=newNode;
tail = newNode;
}
// if linked list is not empty
else{
tail.next=newNode;
tail=newNode;
}
}
// function to traverse the linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public void traverse(){
// if linked list is empty
if(head==null){
System.out.println("Linked List is empty");
return;
}
// else if the linked list is not empty, we take a temp pointer to head and traverse the linked list using temp pointer as we don't want to alter the head pointer
Node temp=head;
while(temp!=null){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println();
}
// function to search for a value in linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public void search(int data){
// if linked list is empty
if(head==null){
System.out.println("Linked List is empty, can't search");
return;
}
// else if linked list is not empty
// take a temp pointer
Node temp=head;
// take a found variable and assign -1 to it. -1 indicates "not found"
int found=-1;
// iterate through the linked list
while(temp!=null){
// if given data is present in linked list
if(temp.data==data){
found=0;
break;
}
// if data is not found in present node then move temp pointer to next node
temp = temp.next;
}
// if found variable's value becomes 0 , means given data is present in linked list
if(found==0){
System.out.println("found "+data+" in linked list");
}
// if found variab;e's value is still -1 means "not found" in linked list
else{
System.out.println("Not found "+data+" in linked list");
}
}
// function to count number of nodes in linked list
// Time Complexity - O(n) , Space Complexity - O(1)
public int nodeCount(){
// take a temp pointer to head
Node temp=head;
// count variable stores the count the no of nodes in linked list
int count=0;
while(temp!=null){
// if current node is not null, we increment count and move to next node , if any
count++;
temp=temp.next;
}
// finally we return the value of count
return count;
}
// function to insert a value at given location
// Time Complexity - O(n) , Space Complexity - O(1)
public void insertNodeByLoc(int data, int loc){
// making a new node with given data
Node newNode = new Node(data);
// take a temp pointer to head
Node temp=head;
// count no of nodes in linked list
int noOfNodes = nodeCount();
// check if the given loc is a valid location to insert or not
if(loc>noOfNodes+1 || loc<=0){ // greater than noOfNodes+1 because loc can be just after //last node which if noOfNodes+1.
System.out.println("Not a valid location to insert");
return;
}
// if we have to insert at start
if(loc==1){
newNode.next=head;
head=newNode;
}
// if we have to insert at end
else if(loc==noOfNodes+1){
tail.next=newNode;
tail=newNode;
}
// if we have to insert anywhere in between
else{
// we first bring the temp pointer just before the desired loc
for(int i=0;i<loc-2;i++){
temp = temp.next;
}
// now we do the new linking
newNode.next=temp.next;
temp.next=newNode;
}
}
// function to delete a node in linked list from the given location
// Time Complexity - O(n) , Space Complexity - O(1)
public void deleteNodeByLoc(int loc){
// if linked list is empty
if(head==null){
System.out.println("Linked list is empty, can't delete");
return;
}
// else if linked list is not empty
// take a temp pointer to head
Node temp = head;
// count the no. of nodes in linked list
int noOfNodes = nodeCount();
// now check the validation of given location
if(loc>noOfNodes || loc<=0){
// if it is not a valid location, return from this function
System.out.println("Not a valid location to delete");
return;
}
// if it is a valid location
// if we have to delete starting node
if(loc==1){
head=head.next;
}
// if we have to delete any other node
else{
// we bring the temp pointer just before the node to delete
for(int i=0;i<loc-2;i++){
temp=temp.next;
}
// now we check if it is last node of linked list, as we have maintained a tail pointer, we have to modify tail's reference if we delete the last node
if(loc==noOfNodes){
temp.next=null;
temp=tail;
}
// if it is not last node and any node in between
else{
temp.next=temp.next.next;
}
}
}
// function to delete a node of linked list having the given value
// Time Complexity - O(n) , Space Complexity - O(1)
public void deleteNodeByValue(int value){
// if linked list is already empty
if(head==null){
System.out.println("Linked list is empty, can't delete");
return;
}
// else if linked list is not empty
// take a temp pointer to head
Node temp= head;
// take a previous node which always remains one node behind temp
Node previousNode=null;
// take a found valiable. -1 indicates "not found"
int found=-1;
// search for the given value and if found bring the temp pointer to the node to be deleted and previousNode pointer to one node before temp
while(temp!=null){
if(temp.data==value){
found=0;
break;
}
previousNode=temp;
temp=temp.next;
}
// if given node not found in linked list
if(found==-1){
System.out.println("Node not present in linked list, hence can't delete");
return;
}
// if given node found at head
if(temp.data==head.data){
head=head.next;
return;
}
// else if given node found anywhere else, do the linking and unlinking
previousNode.next=temp.next;
// one extra check, if given node is found at tail we also have to modify tail's reference after we delete the given node
if(temp.data==tail.data){
previousNode=tail;
}
}
// function to delete complete linked list
// Time Complexity - O(1) , Space Complexity - O(1)
public void deleteCompleteLinkedList(){
// if linked list is already empty
if(head==null){
System.out.println("Linked list is already empty");
return;
}
// if linked list is not empty
head=null;
tail=null;
}
public static void main(String[] args) throws Exception {
SingleLinkedList ll = new SingleLinkedList();
ll.addNode(1);
ll.traverse();
ll.addNode(2);
ll.traverse();
ll.insertNodeByLoc(5,3);
ll.insertNodeByLoc(15,3);
ll.traverse();
ll.search(1);
ll.search(10);
ll.deleteNodeByLoc(1);
ll.traverse();
ll.deleteNodeByValue(54);
ll.traverse();
ll.deleteCompleteLinkedList();
ll.traverse();
}
}
/* ======= Output ==============
1
1 2
1 2 15 5
found 1 in linked list
Not found 10 in linked list
2 15 5
Node not present in linked list, hence can't delete
2 15 5
Linked List is empty
===================================== /*