Data Structures - Linked List | Insertion at the Nth position
In the previous post we had performed the Insertion at the beginning operation in Linked List, which went well when we wanted to insert the element in an empty list or at the beginning of a filled list.
In this post we would look at the Insertion of elements at the Nth position.
Take a look at the following diagram!
The above diagram describes what we are going to do.
To be specific:
In this post we would look at the Insertion of elements at the Nth position.
Take a look at the following diagram!
The above diagram describes what we are going to do.
To be specific:
- We will be creating a Node (temporary node) that we want to insert into the linked list.
- We will also create another node that will store the information of the previous node( the node after which we are going to add our new node).
- Next, we will traverse through the linked list until the node just before the Nth node.
- In the new node we will assign its next pointer to the next pointer of the previous node.
- Also we will update the next pointer of previous node to point to the new node.
- At last we are going to print the list.
While performing the above steps keep in mind that initially the head pointer will be pointing to NULL. We would consider the nodes to be starting from count 1. Therefore for the above example we have 3 Nodes. The new_node will be inserted between the Node number 1 and Node number 2.
Here's the code:
Happy Coding!!

Comments
Post a Comment