11/**
2- * Data-Structures-in -Java
2+ * Data-Structures-In -Java
33 * SinglyLinkedList.java
44 */
55package com .deepak .data .structures .LinkedList ;
@@ -65,8 +65,10 @@ public void insertAtHead(E value) {
6565 */
6666 public void insertAtTail (E value ) {
6767 Node <E > newNode = new Node <E >(value );
68- newNode .next = null ; /* Since this insertion is at tail, this new node will point to null */
69- if (null == head ) { /* When list is empty */
68+ newNode .next = null ;
69+ /* Since this insertion is at tail, this new node will point to null */
70+ if (null == head ) {
71+ /* When list is empty */
7072 head = newNode ;
7173 } else {
7274 Node <E > tempNode = head ;
@@ -95,7 +97,8 @@ public void insertAtTail(E value) {
9597 public void insertAtPosition (E value , int position ) {
9698 if (position < 0 || position > size ) {
9799 throw new IllegalArgumentException ("Position is Invalid" );
98- } /* Conditions check passed, let's insert the node */
100+ }
101+ /* Conditions check passed, let's insert the node */
99102 Node <E > newNode = new Node <E >(value );
100103 if (position == 0 ) {
101104 newNode .next = head ;
@@ -125,7 +128,7 @@ public void traverse() {
125128 /**
126129 * Returns size of the linked list
127130 *
128- * @return
131+ * @return {@link int}
129132 */
130133 public int size () {
131134 return size ;
@@ -134,7 +137,7 @@ public int size() {
134137 /**
135138 * Returns true is list is empty
136139 *
137- * @return
140+ * @return {@link boolean}
138141 */
139142 public boolean isEmpty () {
140143 return size == 0 ;
@@ -146,15 +149,17 @@ public boolean isEmpty() {
146149 * exception is thrown.
147150 *
148151 * @param index
149- * @return
152+ * @return {@link Node<E>}
150153 */
151154 public Node <E > searchByIndex (int index ) {
152155 if (index < 0 || index >= size ) {
153156 throw new IndexOutOfBoundsException ("Invalid index passed while searching for a value" );
154- } /* Validation passed, let's search for value using the index */
157+ }
158+ /* Validation passed, let's search for value using the index */
155159 Node <E > temp = head ;
156- for (int i = 0 ; i < index ; i ++) { /* Start from 0 and go till one less then index
157- because we are jumping to next node inside the loop */
160+ for (int i = 0 ; i < index ; i ++) {
161+ /* Start from 0 and go till one less then index
162+ * because we are jumping to next node inside the loop */
158163 temp = temp .next ;
159164 }
160165 return temp ;
@@ -166,9 +171,10 @@ public Node<E> searchByIndex(int index) {
166171 * in linked list, first one will be returned.
167172 *
168173 * @param value
169- * @return
174+ * @return {@link Node<E>}
170175 */
171- public Node <E > searchByValue (E value ) { /* Traverse through each node until this value is found */
176+ public Node <E > searchByValue (E value ) {
177+ /* Traverse through each node until this value is found */
172178 Node <E > temp = head ;
173179 while (null != temp .next && temp .item != value ) {
174180 temp = temp .next ;
@@ -183,9 +189,11 @@ public Node<E> searchByValue(E value) { /* Traverse through each node until this
183189 * Delete's the element present at head node
184190 */
185191 public void deleteFromHead () {
186- if (null == head ) { /* If list is empty, return */
192+ /* If list is empty, return */
193+ if (null == head ) {
187194 return ;
188195 }
196+ /* Update head and reduce size */
189197 head = head .next ;
190198 size --;
191199 }
@@ -194,16 +202,21 @@ public void deleteFromHead() {
194202 * Delete's the element present at tail node
195203 */
196204 public void deleteFromTail () {
205+ /* If head is null, nothing to delete */
197206 if (null == head ) {
198207 return ;
199208 }
209+ /* Keep a pointer on head node and next node.
210+ * Keep going until next becomes null */
200211 Node <E > currentNode = head ;
201212 Node <E > nextNode = currentNode .next ;
202213 while (currentNode .next != null && nextNode .next != null ) {
203214 currentNode = currentNode .next ;
204215 nextNode = nextNode .next ;
205216 }
217+ /* Now we are removing from tail, so tail - 1 node will point to null */
206218 currentNode .next = null ;
219+ /* Reduce the size */
207220 size --;
208221 }
209222
@@ -215,12 +228,14 @@ public void deleteFromTail() {
215228 public void deleteFromPosition (int position ) {
216229 if (position < 0 || position >= size ) {
217230 throw new IllegalArgumentException ("Position is Invalid" );
218- } /* Conditions check passed, let's delete the node */
231+ }
232+ /* Conditions check passed, let's delete the node */
219233 Node <E > nodeToBeDeleted = head ;
220234 for (int i = 0 ; i < position ; i ++) {
221235 nodeToBeDeleted = nodeToBeDeleted .next ;
222236 }
223- if (nodeToBeDeleted .next == null ) { /* If this is a last node */
237+ if (nodeToBeDeleted .next == null ) {
238+ /* If this is a last node */
224239 deleteFromTail ();
225240 } else {
226241 nodeToBeDeleted .item = nodeToBeDeleted .next .item ;
@@ -232,12 +247,15 @@ public void deleteFromPosition(int position) {
232247 * Returns a array containing each element
233248 * from the list from start to end
234249 *
235- * @return
250+ * @return {@link Object[]}
236251 */
237252 public Object [] toArray () {
253+ /* Create an array of object of same size */
238254 Object [] result = new Object [size ];
239255 int i = 0 ;
240256 for (Node <E > x = head ; x != null ; x = x .next ) {
257+ /* Take each node and add it to the array
258+ * at appropriate position*/
241259 result [i ++] = x .item ;
242260 }
243261 return result ;
0 commit comments