C++ Linked List Exercises: Insert a new node at the end of a Linked List
C++ Linked List: Exercise-5 with Solution
Write a C++ program to insert a new node at the end of a Singly Linked List.
Visualization:

Test Data:
Original Linked list:
13 11 9 7 5 3 1
Insert a new node at the end of a Singly Linked List:
13 11 9 7 5 3 1 0
Sample Solution:
C++ Code:
#include <iostream>
using namespace std;
//Declare node
struct Node{
int num;
Node *next;
};
//Starting (Head) node
struct Node *head=NULL;
//Insert node at start
void insert_Node(int n){
struct Node *new_node=new Node;
new_node->num=n;
new_node->next=head;
head=new_node;
}
void push_back(int newElement) {
Node* new_Node = new Node();
new_Node->num = newElement;
new_Node->next = NULL;
if(head == NULL) {
head = new_Node;
}
else
{
Node* temp = head;
while(temp->next != NULL)
temp = temp->next;
temp->next = new_Node;
}
}
//Display all nodes
void display_all_nodes()
{
struct Node *temp = head;
while(temp!=NULL){
cout<<temp->num<<" ";
temp=temp->next;
}
}
int main(){
insert_Node(1);
insert_Node(3);
insert_Node(5);
insert_Node(7);
insert_Node(9);
insert_Node(11);
insert_Node(13);
cout<<"Original Linked list:\n";
display_all_nodes();
cout<<"\n\nInsert a new node at the end of a Singly Linked List:\n";
push_back(0);
display_all_nodes();
cout<<endl;
return 0;
}
Sample Output:
Original Linked list: 13 11 9 7 5 3 1 Insert a new node at the end of a Singly Linked List: 13 11 9 7 5 3 1 0
Flowchart:


CPP Code Editor:
Contribute your code and comments through Disqus.
Previous C++ Exercise: Insert a new node at the beginning of a Linked List.
Next C++ Exercise: Find middle element in a single linked list.
What is the difficulty level of this exercise?
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join