Repository navigation
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
283 lines (250 loc) · 8.5 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
283 lines (250 loc) · 8.5 KB
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
/*
* Implementation of basic singly pointed linked list.
* Every node has data and a pointer which points at next node, with last node's pointer pointing at null.
* Author - Aakash "Ash" Jain.
* Contact - [email protected].
*/
package Lists;
public class SinglyLinkedList implements LinkedList {
// Node prototype.
private class Node {
// Model data. Data can be any object
// More fields or custom objects can also be added as per requirements.
private Object data;
// Pointer to the next node.
private Node next;
// Constructor.
Node(Object obj, Node next) {
this.data = obj;
this.next = next;
}
}
// First node in the list.
private Node head;
// Last node in the list.
private Node tail;
// Length of the list. (Total number of nodes in the list)
private int len;
// Initialize the list.
public SinglyLinkedList() {
// Not necessary to set just better practice.
this.head = this.tail = new Node(null, null);
this.len = 0;
}
// Add first node when the list is empty. O(1)
private int addFirst(Object obj) {
try {
// Create new node whose pointer points at null and set tail and head both to it.
this.head = this.tail = new Node(obj, null);
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Add node at the start of the list. O(1)
public int addAtStart(Object obj) {
try {
if (this.isEmpty())
return addFirst(obj);
// Create new node whose pointer points at the head node and
// then set head itself to this newly created node.
this.head = new Node(obj, this.head);
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Add node at the end of the list. O(1)
public int addAtEnd(Object obj) {
try {
if (this.isEmpty())
return addFirst(obj);
// Create new node, set current tail's pointer to that node and
// set tail itself to that node. O(1).
this.tail = this.tail.next = new Node(obj, null);
/* If the tail is not set.
Node current = this.head;
// Iterate over the whole list. O(n) (Inefficient)
while(current.next != null)
current = current.next
current.next = new Node(obj, null);
*/
this.len++;
return 0;
} catch (Exception e) {
return 1;
}
}
// Add node at a particular index. Supports negative or overflowing indexes. O(n)
public int insertAt(Object obj, int index) {
try {
// Adjust for negative index.
if (index < 0 && index * (-1) <= this.len)
index = this.len - index * (-1);
// If negative index overflows.
else if (index < 0)
return 1;
// Check to divert to O(1) implementations.
if (index == 0)
return addAtStart(obj);
else if (index >= this.len-1)
return addAtEnd(obj);
Node current = this.head;
Node previous = null;
// Iterate over the list until node just before index is reached.
for (int i = 0; i < index; i++) {
previous = current;
current = current.next;
}
// Create new node whose pointer points at node situated at index then
// set previous node's pointer to this newly created node.
previous.next = new Node(obj, current);
this.len++;
return 0;
} catch (Exception e) {
return -1;
}
}
// Return node data at a specified index. Supports negative or overflowing indexes. O(n)
public Object getAt(int index) {
try {
// Adjust for negative index.
if (index < 0 && index * (-1) <= this.len)
index = this.len - index * (-1);
// If negative index overflows.
else if (index < 0)
return null;
if (index > this.len-1)
index = this.len-1;
Node current = this.head;
// Iterate over the list until index is reached and return the data.
for (int i = 0; i < index; i++)
current = current.next;
return current.data;
} catch (Exception e) {
return null;
}
}
// Remove node at specified index. Does NOT support negative or overflowing indexes. O(n)
public int removeAt(int index) {
try {
// Check for invalid index.
if (index < 0 || index >= this.len)
return 1;
// If there's only one element in the list.
else if (this.len == 1) {
this.head.data = null;
this.head = this.tail = new Node(null, null);
}
else if (index == 0) {
this.head.data = null;
this.head = this.head.next;
}
else {
Node current = this.head;
Node previous = null;
// Iterate over the list until node at index is reached.
for (int i = 0; i < index; i++) {
previous = current;
current = current.next;
}
// Nullify the data and modify pointer of previous node to
// the node next to it.
current.data = null;
previous.next = current.next;
if (index == this.len-1)
this.tail = current;
}
this.len--;
return 0;
} catch (Exception e) {
return -1;
}
}
// Delete list. O(n)
public int clear() {
try {
Node current = this.head;
// Iterate over the list while setting data fields to null.
while (current != null) {
current.data = null;
current = current.next;
}
// Set head and tail to empty node and call in Java garbage collector.
this.head = this.tail = new Node(null, null);
System.gc();
this.len = 0;
return 0;
} catch (Exception e) {
return 1;
}
}
// Return true if list is empty, false otherwise. O(1)
public boolean isEmpty() {
return this.head.data == null && this.len == 0;
}
// Return length of the list. O(1)
public int getLength() {
return this.len;
}
// Stringify the linked list data. O(n)
@Override
public String toString() {
try {
if (this.isEmpty())
return "[]\n";
Node current = this.head;
String result = "[";
while (current != null) {
// Add quotes if the object is a string.
if (current.data instanceof String)
result += "\"" + current.data + "\", ";
else
result += current.data + ", ";
current = current.next;
}
// Remove 2 unnecessary characters from the last node.
return result.substring(0, result.length()-2) + "]\n";
} catch (Exception e) {
return null;
}
}
// Print the linked list data.
@Deprecated
public int printList() {
try {
if (this.isEmpty())
System.out.println("THE LIST IS EMPTY:\n[]\n");
else {
System.out.print("\nDATA IN LINKED LIST IS: \n[");
Node current = this.head;
while(current != null) {
System.out.print(current.data);
if (current.next != null)
System.out.print(", ");
current = current.next;
}
System.out.println("]\n");
}
return 0;
} catch (Exception e) {
return 1;
}
}
/*
@Deprecated
public Object pop(int index) {
// Check for invalid index.
if (index < 0 || index >= this.len)
return null;
Object data = getAt(index);
removeAt(index);
return data;
}
*/
// Tests.
public static void main(String[] args) {
}
}