33/**
44 * <p></p>
55 * 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>
78 * (e.g being able to branch off and create a new Linked List in O(1)
89 * rather than iterating to find size of branched off list)
910 * <p></p>
1011 * Constructors: <br>
1112 * 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.
1619 * <p></p>
1720 * Callable methods are: <br>
1821 * size() -- Gets the size of the linked list <br>
1922 * insertFront(T object) -- inserts the object at the front of the linked list <br>
2023 * insertEnd(T object) -- inserts the object at the end of the linked list <br>
2124 * insert(T object, int idx) -- inserts the object at the specified index of the linked list <br>
2225 * 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>
2428 * pop() -- remove the last node from the linked list <br>
2529 * 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>
2732 * 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>
2935 * sort() -- sorts the linked list by their natural order <br>
3036 *
3137 * @param <T> generic type for objects to be stored in the linked list
3238 */
3339public class LinkedList <T extends Comparable <T >> {
3440 private Node <T > head ;
3541 private int size ;
36-
42+
43+ /**
44+ * Constructor for linkedlist.
45+ */
3746 public LinkedList () {
3847 head = null ;
3948 size = 0 ;
4049 }
50+ /**
51+ * Overloaded constructor for linkedlist.
52+ *
53+ * @param head Head of the linked list.
54+ */
4155
4256 public LinkedList (Node <T > head ) {
4357 this .head = head ;
@@ -57,16 +71,19 @@ private LinkedList(Node<T> head, int size) {
5771
5872 /**
5973 * Gets the size of the linked list.
74+ *
6075 * @return int value
6176 */
6277 public int size () {
6378 return this .size ;
6479 }
6580
6681 /**
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+ *
6884 * @param object to be inserted
6985 * @param idx index which the object is to be inserted into
86+ *
7087 * @return boolean representing whether insertion was successful
7188 */
7289 public boolean insert (T object , int idx ) {
@@ -102,26 +119,32 @@ public boolean insert(T object, int idx) {
102119 }
103120
104121 /**
105- * Inserts the object at the front of the linked list
122+ * Inserts the object at the front of the linked list.
123+ *
106124 * @param object to be inserted
125+ *
107126 * @return boolean representing whether insertion was successful
108127 */
109128 public boolean insertFront (T object ) {
110129 return insert (object , 0 );
111130 }
112131
113132 /**
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+ *
116137 * @return boolean representing whether insertion was successful
117138 */
118139 public boolean insertEnd (T object ) {
119140 return insert (object , this .size );
120141 }
121142
122143 /**
123- * remove the node at the specified index
144+ * remove the node at the specified index.
145+ *
124146 * @param idx of the node to be removed
147+ *
125148 * @return node's value
126149 */
127150 public T remove (int idx ) {
@@ -148,8 +171,10 @@ public T remove(int idx) {
148171 }
149172
150173 /**
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+ *
152176 * @param object
177+ *
153178 * @return index of the node found
154179 */
155180 public int search (T object ) {
@@ -171,8 +196,10 @@ public int search(T object) {
171196 }
172197
173198 /**
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+ *
175201 * @param object to search and delete
202+ *
176203 * @return boolean whether the delete op was successful
177204 */
178205 public boolean delete (T object ) {
@@ -185,15 +212,17 @@ public boolean delete(T object) {
185212 }
186213
187214 /**
188- * remove the last node from the linked list
215+ * remove the last node from the linked list.
216+ *
189217 * @return value of popped node
190218 */
191219 public T pop () {
192220 return remove (size () - 1 );
193221 }
194222
195223 /**
196- * remove the first node from the linked list
224+ * remove the first node from the linked list.
225+ *
197226 * @return value of polled node
198227 */
199228 public T poll () {
@@ -202,8 +231,10 @@ public T poll() {
202231
203232
204233 /**
205- * get the node at the specified index
234+ * get the node at the specified index.
235+ *
206236 * @param idx of node to be found
237+ *
207238 * @return node found
208239 */
209240 public Node <T > get (int idx ) {
@@ -220,10 +251,11 @@ public Node<T> get(int idx) {
220251 /**
221252 * reverse the linked list.
222253 * 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>.
224255 */
225256 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 ) {
227259 return ;
228260 }
229261
@@ -245,11 +277,11 @@ public void reverse() {
245277 * Sorts the linked list by the natural order of the elements.
246278 * Generally, merge sort is the most efficient sorting algorithm for linked lists.
247279 * 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.
250282 *
251283 * 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>.
253285 */
254286 public void sort () {
255287 if (this .size <= 1 ) {
@@ -268,9 +300,11 @@ public void sort() {
268300 }
269301
270302 /**
271- * Merge routine helper function for two sorted linked lists
303+ * Merge routine helper function for two sorted linked lists.
304+ *
272305 * @param first sorted linked list
273306 * @param second sorted linked list
307+ *
274308 * @return head of merged linked list
275309 */
276310 private Node <T > merge (LinkedList <T > first , LinkedList <T > second ) {
@@ -314,7 +348,7 @@ public String toString() {
314348 }
315349
316350 /**
317- * Node class for linked list
351+ * Node class for linked list.
318352 */
319353 public static class Node <T > {
320354 T val ;
0 commit comments