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

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>