![]() |
VOOZH | about |
Inserting a node into a linked list can be done in several ways, depending on where we want to insert the new node. Here, we'll cover four common scenarios: inserting at the front of the list, after a given node, at a specific position, and at the end of the list
Table of Content
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.
To insert a node at the front of the linked list, follow these steps:
next pointer of the new node to the current head of the list.Below is the implementation of the approach:
Original Linked List: 2 3 4 5 After inserting Nodes at the front: 1 2 3 4 5
Time Complexity: O(1), We have a pointer to the head and we can directly attach a node and update the head pointer. So, the Time complexity of inserting a node at the head position is O(1).
Auxiliary Space: O(1)
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.
To insert a node after a specific node:
next pointer of the new node to the next pointer of the given node.next pointer of the given node to the new node.Below is the implementation of the approach:
Original Linked List: 2 3 5 6 Linked List after insertion: 2 3 4 5 6
Time complexity: O(N), where N is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
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 points to the new node.
To insert a node at a specific position:
next pointers to insert the new node at the desired position.Below is the implementation of the approach:
Linked list before insertion: 3 5 8 10 Linked list after insertion of 12 at position 3: 3 12 5 8 10
Time complexity: O(N), where N is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
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.
To insert a node at the end of the linked list:
next pointer of the last node to the new node.Below is the implementation of the approach:
Created Linked list is: 2 3 4 5 6 After inserting 1 at the end: 2 3 4 5 6 1
Time complexity: O(N), where N is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work.
Auxiliary Space: O(1)
Please refer complete article on Insertion in Linked List for more details!