How to insert a node at the end of a given node?

Article by: Manish Methani

Last Updated: October 10, 2021 at 2:04pm IST
2 min 40 sec read

Linked List insertion can be done using three ways,

1) Insertion of a new node at front of a given node.

2) Insertion of a new node after a given node.

3) Insertion of a new node at the end of the node.

In this tutorial, we will see how to insert a new node after a given node in Linked List. To understand this lets see the first scenario in which the original Linked list contains a head pointer pointing to First Node. The first node is pointing towards the second Node containing value 2. Second Node point towards the third node containing value 3. At last, the third Node points towards  NULL.

1) Before Insertion of the node at the end

1) Original Linked list contains a head pointer pointing to First Node.

2) First Node points to second Node containing value 2.

3) Second Node points to the third node containing value 3.

4) Third Node points to NULL.

2) After Insertion of the node at the end

Now if we want to insert a node at the end of a given node of a linked list, an algorithm for that would be like this,

1) Head point towards the first Node containing value 1.

2) First Node points to the second Node containing value 2.

3) Second Node points to the third node containing value 3.

4) New Node's next points to NULL.

5) Third node's next points to New Node.

Now, have a look at the second scenario in which we are going to insert a node after a given node. For that thing to happen, Head point towards the first Node containing value 1. Then the first Node points to the second Node containing value 2. Then the second node points to the third node containing value 3. New Node's next points to NULL. At last, third node's next points to New Node.

So, this is how we can manipulate the nodes to insert and get the results in the linked list. Lets see a C function on how to insert a node at the end of a given node in the linked list.

Function to insert node after a given node of Linked List :

void insertNodeAtend(struct node** head_ref, int new_data)
{
  /* 1. allocate node */
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
 
    struct node *last = *head_ref;  /* used in step 5*/
  
  /* 2. put in the data  */
    new_node->data  = new_data;
 
  /* 3. This new node is going to be the last node, so make next 
          of it as NULL*/ 
    new_node->next = NULL;
 
  /* 4. If the Linked List is empty, then make the new node as head */
    if (*head_ref == NULL)
    {
       *head_ref = new_node;
       return;
    }  
      
  /* 5. Else traverse till the last node */
    while (last->next != NULL)
        last = last->next;
  
  /* 6. Change the next of last node */
    last->next = new_node;
    return;    
}

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com