Insertion in Linked List (original) (raw)

Last Updated : 18 Feb, 2025

Try it on GfG Practice redirect icon

Insertion in a linked list involves adding a new node at a specified position in the list. There are several types of insertion based on the position where the new node is to be added:

1. Insert a Node at the Front/Beginning of the Linked List

To insert a new node at the front, we create a **new node and point its **next reference to the **current head of the linked list. Then, we update the **head to be this new node. This operation is efficient because it only requires adjusting a few pointers.

Insertion-at-the-Beginning-of-Singly-Linked-List

Insert a Node at the Front/Beginning of Linked List

**Algorithm:

To read more about insert a new node at the front Refer, Insert a Node at Front/Beginning of a Linked List

2. Insert a Node after a Given Node in Linked List

If we want to insert a new node after a specific node, we first locate that node. Once we find it, we set the **new node's next reference to point to the node that follows the given node. Then, we update the given node's next to point to the new node. This requires traversing the list to find the specified node.

Insert-a-node-after-a-given-node-in-Linked-List

Insertion after a given node in Linked List

**Algorithm:

To read more about insert a new node after a specific node Refer, Insert a Node after a given Node in Linked List

3. Insert a Node before a Given Node in Linked List

If we want to insert a new node **before a given node, we first locate that node while keeping the track of **previous node also. Once we find it, we set the previous node's **next reference the new node. Then, we update the node's next reference to point to the **given node.

Insert-a-Node-before-a-Given-Node-in-Linked-List

**Algorithm:

To read more about insert a new node before a given node Refer, Insert a node in Linked List before a given node

4. Insert a Node At a Specific Position in Linked List

To insert a new node at a specific position, we need to traverse the list to position - 1. If the position is valid, we adjust the pointers similarly such that the next pointer of the **new node points to the next of **current nod and **next pointer of **current node pointsto the new node.

Insertion-at-a-Specific-Position-of-the-Singly-Linked-List-copy

Insertion at specific position in Linked List

**Algorithm:

To read more about insert a new node at a specific position Refer, Insert a node at a specific position in a linked list

5. Insert a Node at the End of Linked List

Inserting at the end involves traversing the entire list until we reach the last node. We then set the last node's next reference to point to the new node, making the **new node the last element in the list.

Insertion-at-the-End-of-Singly-Linked-List

Insertion at end of Linked List

**Algorithm:

To read more about inserting at the end Refer, Insert Node at the End of a Linked List