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

2 min 40 sec read

Author: Manish Methani

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;    
}

How was the tutorial? Nice. Right?



Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.



What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp


Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python