# Insert a node at the tail of a linked list: Program and Algorithm

In this article, you’ll learn how to insert a node at the tail of a linked list in C++, along with that you’ll also see the algorithm to insert a node at the tail/end of a linked list.

So, Let’s get started!

Prerequisite: You should have the knowledge of How do you create a node in a linked list? and How to traverse a singly linked list?

## How do you insert a node at the tail of a linked list?

Let’s see how you can insert a node at the tail of a linked list step-by-step:

Variables we are using here:

1. head: A pointer of `Node` type pointing to the first node of the linked list.
2. temp: A pointer of `Node` type used to traverse the list till the end.
3. tail: The node to be inserted (It will also be of `Node` type).

The following algorithm inserts a node at the tail of a linked list:

## Algorithm to insert a node at the tail of a linked list:

• Step 01: Start
• Step 02: Create a new node named `tail` of `Node` type with the given data.
• Step 03: If `head == NULL`
• Step 04: Set `head = tail`
• Step 05: Go to step `13`
• Step 06: [End of If ]
• Step 07: Else
• Step 08: Declare a pointer named `temp` of `Node` type and initialize it with `head`.
• Step 09: Repeat Step `10` Until `temp →``next``!= NULL`
• Step 10: Set `temp = temp -> next`
• Step 11: Set `temp → next = tail`
• Step 12: [End of Else block. ]
• Step 13: Stop

## Implementation in C++:

Case 01: If the linked list is empty.

```// writing a program in C++ to insert a node at the tail of a linked list

#include<iostream>
using namespace std;

// creating a class "Node"
class Node
{
public:
int data;
Node *next;

//constructor
Node(int data)
{
this -> data = data;
this -> next = NULL;
}
};

{
cout << "List is empty" << endl;
else
{

while(temp != NULL)
{
cout << temp -> data << " -> " ;
temp = temp -> next;
}
cout << "Null" << endl;
}
}

// function to insert a node at the tail of a linked list
{
//creation of a new node
Node* tail = new Node(data);

//empty list

else
{

while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = tail;
}

}

//Driver's code

int main()
{
// if list is empty

return 0;

}

```

If you compile and run the above program, it will produce the following result:

Output:

```7 -> Null

```

Case 02: If the linked list is not empty.

```// writing a program in C++ to insert a node at the tail of a linked list

#include<iostream>
using namespace std;

// creating a class "Node"
class Node
{
public:
int data;
Node *next;

//constructor
Node(int data)
{
this -> data = data;
this -> next = NULL;
}
};

{
cout << "List is empty" << endl;
else
{

while(temp != NULL)
{
cout << temp -> data << " -> " ;
temp = temp -> next;
}
cout << "Null" << endl;
}
}

// function to insert a node at the tail of a linked list
{
//creation of a new node
Node* tail = new Node(data);

//empty list

else
{

while(temp -> next != NULL)
{
temp = temp -> next;
}
temp -> next = tail;
}

}

//Driver's code

int main()
{
// if list is not empty (As you can see, initially this linked list contains a single node i.e., node1)

Node* node1 = new Node(3);

return 0;

}

```

If you compile and run the above program, it will produce the following result:

Output:

```3 -> 11 -> 27 -> 99 -> Null

```

## Conclusion:

In this article, you learned how to insert a node at the tail of a linked list in C++, you also became familiar with the algorithm to insert a node at the tail of a linked list.

So,

I really hope that you liked my article and got a lot of value from it.