Insertion of a new node & Deletion of an existing node
After an exhilarating commencement of the Linked List series, in this post, we'll be exploring the artistry behind insertion and deletion operations in linked list. So let's now move on to the second problem statement of the series, i.e., write two functions for insertion and deletion of a node respectively, from linked list.
Algorithm for inserting a node at pos position:
- Create a function insert () which takes head of the node, pos and the new node as the parameter.
- Take a base case, if pos==1, means the new node is to be made the head of the linked list.
- If the above case is not true, traverse the linked list and reach to position, pos-1.
- Assign the new node with the address of the node at pos.
- Finally, assign the node at pos-1 with address of new node.
Algorithm for deleting a node at pos position:
- Create a function deletion () which takes pos and the head of the LinkedList as parameter.
- Base case, if pos ==1, then assign the second element as the head and return the new head.
- Traverse the linked list till the node at pos-1.
- Assign the node at pos-1 with the address of node with address of the node at pos+1.
Source Code with Explanation:
Insert() function:
It is the function that inserts a new node `nn` into a linked list at a given position `pos`. The method takes three arguments: the head of the linked list `head`, the position `pos` where the new node `nn` should be inserted, and the new node `nn` itself.
The method first checks if the given position is 1. If it is, the method sets the next pointer of the new node to the current head of the list, calls a method named `traverse` with the new node as an argument, and returns the new node as the new head of the list.
If the given position is not 1, the method iterates through the list until it reaches the node before the desired position. It then sets the next pointer of the new node to the next node of the current node and sets the next pointer of the current node to the new node. Finally, it returns the head of the list.
Note: It's important to note that this code assumes that the given position is valid and within the bounds of the list.
Time Complexity: The time complexity of the insert() function is O(n). The for loop
in the function runs for `pos-1` times where `pos` is the position at which the
new node `nn` is to be inserted. In the worst-case scenario, `pos` can be equal
to the length of the linked list, making the time complexity O(n).
Deletion() function:
It is a method for deleting a node from a linked list at a specified position. The method takes two arguments: `head`, which is the head of the linked list, and `pos`, which is the position of the node to be deleted.
First, the method creates a new `Node` object called `cur` and sets it equal to the `head` of the linked list. If the position to be deleted is 1 (i.e., the first node in the list), then the method sets the `head` of the list to be the next node in the list.
If the position to be deleted is not 1, then the method enters a `for` loop that iterates from 1 to `pos-1`. In each iteration of the loop, `cur` is set to be the next node in the list. After the loop completes, `cur` is pointing to the node immediately before the node to be deleted. The method then sets the `next` field of `cur` to be equal to the node after the node to be deleted.
Finally, after either deleting the first node or deleting a node at some other position in the list, the method calls a function named `traverse(head)` which traverses and prints out all elements of the linked list starting from its head.
Time Complexity: The time complexity of the deletion() function is O(n). The reason is somewhat similar to that of insert() function. In worst case scenario, the for loop will traverse the full length of the linked list, , making the time complexity O(n).
Full source code link: Source Code | Insertion and Deletion of Linked List | MyCodingNetwork
Output:
The first line is the original Linked List.
The second line is after insertion operation is performed, 50 is added at 3rd position.
The third line is after deletion operation is performed, first element is deleted.