3
3
/**
4
4
* <p></p>
5
5
* There are many variations when it comes to implementing linked lists. Here's ours. <br>
6
- * A linked list that tracks both head and tail may improve the complexity of some methods below <br>
6
+ * A linked list that tracks both head and tail may improve the complexity
7
+ * of some methods below <br>
7
8
* (e.g being able to branch off and create a new Linked List in O(1)
8
9
* rather than iterating to find size of branched off list)
9
10
* <p></p>
10
11
* Constructors: <br>
11
12
* LinkedList() -- Initialise a link list with a null head; size = 0 <br>
12
- * LinkedList(Node<T> head) -- given a node, make node the head of a new linked list;
13
- * size is found by iterating to the end from this specified head <br>
14
- * LinkedList(Node<T>head, int size) -- given a head node and size of linked list specified;
15
- * made private to avoid client from constructing a linked list with invalid size
13
+ * LinkedList(Node head) -- given a node, make node the head of a new linked list;
14
+ * size is found by iterating to the end from this
15
+ * specified head <br>
16
+ * LinkedList(Node head, int size) -- given a head node and size of linked list specified;
17
+ * made private to avoid client from constructing a
18
+ * linked list with invalid size.
16
19
* <p></p>
17
20
* Callable methods are: <br>
18
21
* size() -- Gets the size of the linked list <br>
19
22
* insertFront(T object) -- inserts the object at the front of the linked list <br>
20
23
* insertEnd(T object) -- inserts the object at the end of the linked list <br>
21
24
* insert(T object, int idx) -- inserts the object at the specified index of the linked list <br>
22
25
* remove(int idx) -- remove the node at the specified index <br>
23
- * delete(T object) -- delete the 1st encounter of the specified object from the linked list <br>
26
+ * delete(T object) -- delete the 1st encounter of the specified object
27
+ * from the linked list <br>
24
28
* pop() -- remove the last node from the linked list <br>
25
29
* poll() -- remove the first node from the linked list <br>
26
- * search(T object) -- search for the 1st encounter of the node that holds the specified object <br>
30
+ * search(T object) -- search for the 1st encounter of the node that
31
+ * holds the specified object <br>
27
32
* get(int idx) -- get the node at the specified index <br>
28
- * reverse() -- reverse the linked list (head of linked list now starts from the back) <br>
33
+ * reverse() -- reverse the linked list
34
+ * (head of linked list now starts from the back) <br>
29
35
* sort() -- sorts the linked list by their natural order <br>
30
36
*
31
37
* @param <T> generic type for objects to be stored in the linked list
32
38
*/
33
39
public class LinkedList <T extends Comparable <T >> {
34
40
private Node <T > head ;
35
41
private int size ;
36
-
42
+
43
+ /**
44
+ * Constructor for linkedlist.
45
+ */
37
46
public LinkedList () {
38
47
head = null ;
39
48
size = 0 ;
40
49
}
50
+ /**
51
+ * Overloaded constructor for linkedlist.
52
+ *
53
+ * @param head Head of the linked list.
54
+ */
41
55
42
56
public LinkedList (Node <T > head ) {
43
57
this .head = head ;
@@ -57,16 +71,19 @@ private LinkedList(Node<T> head, int size) {
57
71
58
72
/**
59
73
* Gets the size of the linked list.
74
+ *
60
75
* @return int value
61
76
*/
62
77
public int size () {
63
78
return this .size ;
64
79
}
65
80
66
81
/**
67
- * inserts the object at the specified index of the linked list
82
+ * inserts the object at the specified index of the linked list.
83
+ *
68
84
* @param object to be inserted
69
85
* @param idx index which the object is to be inserted into
86
+ *
70
87
* @return boolean representing whether insertion was successful
71
88
*/
72
89
public boolean insert (T object , int idx ) {
@@ -102,26 +119,32 @@ public boolean insert(T object, int idx) {
102
119
}
103
120
104
121
/**
105
- * Inserts the object at the front of the linked list
122
+ * Inserts the object at the front of the linked list.
123
+ *
106
124
* @param object to be inserted
125
+ *
107
126
* @return boolean representing whether insertion was successful
108
127
*/
109
128
public boolean insertFront (T object ) {
110
129
return insert (object , 0 );
111
130
}
112
131
113
132
/**
114
- * Inserts the object at the end of the linked list
115
- * @param object to be inserted
133
+ * Inserts the object at the end of the linked list.
134
+ *
135
+ * @param object to be inserted
136
+ *
116
137
* @return boolean representing whether insertion was successful
117
138
*/
118
139
public boolean insertEnd (T object ) {
119
140
return insert (object , this .size );
120
141
}
121
142
122
143
/**
123
- * remove the node at the specified index
144
+ * remove the node at the specified index.
145
+ *
124
146
* @param idx of the node to be removed
147
+ *
125
148
* @return node's value
126
149
*/
127
150
public T remove (int idx ) {
@@ -148,8 +171,10 @@ public T remove(int idx) {
148
171
}
149
172
150
173
/**
151
- * search for the 1st encounter of the node that holds the specified object
174
+ * search for the 1st encounter of the node that holds the specified object.
175
+ *
152
176
* @param object
177
+ *
153
178
* @return index of the node found
154
179
*/
155
180
public int search (T object ) {
@@ -171,8 +196,10 @@ public int search(T object) {
171
196
}
172
197
173
198
/**
174
- * delete the 1st encounter of the specified object from the linked list
199
+ * delete the 1st encounter of the specified object from the linked list.
200
+ *
175
201
* @param object to search and delete
202
+ *
176
203
* @return boolean whether the delete op was successful
177
204
*/
178
205
public boolean delete (T object ) {
@@ -185,15 +212,17 @@ public boolean delete(T object) {
185
212
}
186
213
187
214
/**
188
- * remove the last node from the linked list
215
+ * remove the last node from the linked list.
216
+ *
189
217
* @return value of popped node
190
218
*/
191
219
public T pop () {
192
220
return remove (size () - 1 );
193
221
}
194
222
195
223
/**
196
- * remove the first node from the linked list
224
+ * remove the first node from the linked list.
225
+ *
197
226
* @return value of polled node
198
227
*/
199
228
public T poll () {
@@ -202,8 +231,10 @@ public T poll() {
202
231
203
232
204
233
/**
205
- * get the node at the specified index
234
+ * get the node at the specified index.
235
+ *
206
236
* @param idx of node to be found
237
+ *
207
238
* @return node found
208
239
*/
209
240
public Node <T > get (int idx ) {
@@ -220,10 +251,11 @@ public Node<T> get(int idx) {
220
251
/**
221
252
* reverse the linked list.
222
253
* A good video to visualise this algorithm can be found
223
- * <a = https://www.youtube.com/watch?v=D7y_hoT_YZI, href = "url ">here</a>.
254
+ * <a, href = " https://www.youtube.com/watch?v=D7y_hoT_YZI">here</a>.
224
255
*/
225
256
public void reverse () {
226
- if (head == null || head .next == null ) { // No need to reverse if list is empty or only 1 element.
257
+ // No need to reverse if list is empty or only 1 element.
258
+ if (head == null || head .next == null ) {
227
259
return ;
228
260
}
229
261
@@ -245,11 +277,11 @@ public void reverse() {
245
277
* Sorts the linked list by the natural order of the elements.
246
278
* Generally, merge sort is the most efficient sorting algorithm for linked lists.
247
279
* Accessing a random node in the linked list incurs O(n) time complexity.
248
- * This makes sort algorithms like quicksort and heapsort inefficient since they rely
249
- * on the O(1) lookup time, like in an array.
280
+ * This makes sort algorithms like quicksort and heapsort
281
+ * inefficient since they rely on the O(1) lookup time, like in an array.
250
282
*
251
283
* A good video to visualise this algorithm can be found
252
- * <a = https://www.youtube.com/watch?v=JSceec-wEyw, href = "url ">here</a>.
284
+ * <a, href = " https://www.youtube.com/watch?v=JSceec-wEyw">here</a>.
253
285
*/
254
286
public void sort () {
255
287
if (this .size <= 1 ) {
@@ -268,9 +300,11 @@ public void sort() {
268
300
}
269
301
270
302
/**
271
- * Merge routine helper function for two sorted linked lists
303
+ * Merge routine helper function for two sorted linked lists.
304
+ *
272
305
* @param first sorted linked list
273
306
* @param second sorted linked list
307
+ *
274
308
* @return head of merged linked list
275
309
*/
276
310
private Node <T > merge (LinkedList <T > first , LinkedList <T > second ) {
@@ -314,7 +348,7 @@ public String toString() {
314
348
}
315
349
316
350
/**
317
- * Node class for linked list
351
+ * Node class for linked list.
318
352
*/
319
353
public static class Node <T > {
320
354
T val ;
0 commit comments