Last Updated: 06 October, 2024 7 Mins Read
In this Linked List tutorial, we will see all about the insert operation in a single linked list. In the insert operation, we will add a new node to the linked list at any position.
In a Singly Linked List, we can insert a node at the following positions:
At the beginning of the linked list.
At a specific position of the linked list.
After a given node of the linked list.
At the end of the linked list.
To insert a new node at the beginning or front position in the linked list, follow these steps:
Create a newNode with given value.
Check whether list is Empty (head == NULL).
If list is Empty then, set newNode→next = NULL and head = newNode.
If list is Not Empty then, set newNode→next = head and head = newNode.
In the below given example we will see how to insert a new node in the beginning or front the linked list.
Output
Linked List values:
40 60 80 100
After inserting a Node at the beginning:
20 40 60 80 100
To insert a new node at the specific position of the linked list, we need to follow the given steps.
Create a newNode with given value.
Check whether list is Empty (head == NULL).
If list is Empty then, set newNode → next = NULL and head = newNode.
If list is Not Empty then, define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).
Every time check whether temp is reached to last node or not. If it is reached to last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp to next node.
Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'
In the below given example we will see how to insert a new node in the specific position in the linked list.
Output
Linked list before inserting a new node:
10 20 40 50
Linked list after inserting 30 at the position 3:
10 20 30 40 50
To insert a new node after a given node in the linked list, we need to follow the given steps.
Create a newNode with given value.
Check whether list is Empty (head == NULL).
If list is Empty then, set newNode → next = NULL and head = newNode.
If list is Not Empty then, define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).
Every time check whether temp is reached to last node or not. If it is reached to last node then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the function. Otherwise move the temp to next node.
Finally, Set 'newNode → next = temp → next' and 'temp → next = newNode'
In the below given example we will see how to insert a new node after the given node in the linked list.
Output
Linked List data:
10 20 40 50
After insertion, Linked List data:
10 20 30 40 50
To insert a new node at the end or last position in the linked list, follow these steps:
Create a newNode with given value and newNode → next as NULL.
Check whether list is Empty (head == NULL).
If list is Empty then, set head = newNode.
If list is Not Empty then, define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next is equal to NULL).
Set temp → next = newNode.
In the below given example we will see how to insert a new node at the end of the linked list.
Output
Linked list values: 10 20 30 40
Linked List values, after inserting at the end: 10 20 30 40 50
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com