### Online Classes

Results can only be achieved if you have the focused direction and crystal clear knowledge. To achieve this, you need a mentor. We will help you out by connecting with an expert mentor in the field.

Learn More

Article

# How to insert a new node at the end of a node in a linked list? - Codzify.com

2 min 40 sec read

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

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

## Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

## HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor