Singly Linked List Insertion

Let's look at the JavaScript implementation for the insertion of a node in a linked list.

Types of Insertion #

The three types of insertion strategies used in singly linked-list are:

  1. Insertion at the head
  2. Insertion at the tail
  3. Insertion at the N-th index

Insertion at Head #

This type of insertion means that we want to insert a new element as the first element of the list. As a result, the head will point to the newly​ added node, whose nextElement, in turn, will point to the node previously pointed by the head or null value.

The following figure shows how insertAtHead() happens in singly linked-list:

Insertion at Tail #

This type of insertion means that we want to insert a new element as the last element of the list. The original tail element of the list has a nextElement that points to NULL. To insert a new tail node, we have to point the nextElement of the previous tail node to the new tail node, and the nextElement of the new tail now points to NULL. The following figure illustrates this insertion.

Insert at the N-th Node

...