Linked List Revision in JAVA

Today we go through the basics of linked lists in Java.

Firstly we learn about how an element is added to the first position as head:

Steps: S-1: Create a new Node.

S-2: Check if the head is equal to null, if null then make tail equals head equals newNode and return. If not, go through Step 3.

S-3: newNode-->next = head

S-4: head = newNode

Second, we move on to how an element is added to the last position as the tail:

Steps: S-1: Create a new Node.

S-2: Check if the head is equal to null, if null then make tail equals head equals newNode and return. If not, go through Step 3.

S-3: tail-->next = newNode

S-4: tail = newNode

Third, we have to remove the element from the head:

S-1: Store the value of the head in the temp variable.

S-2: Update the value of head as head = head-->next

S-3: then return the temp variable. (Garbage collector in JAVA collects the data of head)

Fourth, we see how the new node is added in the middle:

S-1 = Create a new Node and named it NN.

S-2 = Intialise the new variable named "i", i=0

S-3 = Store the head in the variable temp

S-3 = Repeat steps 4 and 5 until (i < index-1)

S-4 = update the variable temp as temp = temp-->next

S-5 = update i as i = i+1;

S-6 = NN--next = temp-->next

S-7 = temp-->next = NN