We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.
All programs discussed in this post consider the following representations of the linked list.
C++
class Node
{
public :
int data;
Node *next;
};
|
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways
1) At the front of the linked list
2) After a given node.
3) At the end of the linked list.
Add a node at the front: (4 steps process)
The new node is always added before the head of the given Linked List. And newly added node becomes the new head of the Linked List. For example, if the given Linked List is 10->15->20->25 and we add an item 5 at the front, then the Linked List becomes 5->10->15->20->25. Let us call the function that adds at the front of the list is push(). The push() must receive a pointer to the head pointer, because push must change the head pointer to point to the new node (See this)

Here is an example of creating a linked list of 4 nodes using class:
C++
#include <iostream>
using namespace std;
class node {
public :
int data;
node* next;
node( int d){
data = d;
next = NULL;
}
};
void insertAthead(node*& head, int data)
{
node* n = new node(data);
n->next = head;
head = n;
}
void print(node* head)
{
while (head != NULL) {
cout << head->data << "->" ;
head = head->next;
}
}
int main()
{
node* head = NULL;
insertAthead(head, 5);
insertAthead(head, 2);
insertAthead(head, 8);
insertAthead(head, 3);
print(head);
}
|
Explanation: We are inserting the elements 3, 8, 2, and 5 at the start of the linked list, it will give the output as − 3→ 8→ 2→ 5 →.
Time complexity of push() is O(1) as it does a constant amount of work.
Add a node after a given node: (5 steps process)
We are given a pointer to a node, and the new node is inserted after the given node.
C++
void insertAfter(Node* prev_node, int new_data)
{
if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL" ;
return ;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
|
Time complexity of insertAfter() is O(1) as it does a constant amount of work.
Add a node at the end: (6 steps process)
The new node is always added after the last node of the given Linked List. For example if the given Linked List is 5->10->15->20->25 and we add an item 30 at the end, then the Linked List becomes 5->10->15->20->25->30.
Since a Linked List is typically represented by the head of it, we have to traverse the list till the end and then change the next to last node to a new node.

Following are the 6 steps to add node at the end.
C++
void append(Node** head_ref, int new_data)
{
Node* new_node = new Node();
Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return ;
}
|
Time complexity of append is O(n) where n is the number of nodes in the linked list. Since there is a loop from head to end, the function does O(n) work.
This method can also be optimized to work in O(1) by keeping an extra pointer to the tail of linked list/
Following is a complete program that uses all of the above methods to create a linked list.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
int data;
Node *next;
};
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void insertAfter(Node* prev_node,
int new_data)
{
if (prev_node == NULL)
{
cout << "the given previous node cannot be NULL" ;
return ;
}
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void append(Node** head_ref,
int new_data)
{
Node* new_node = new Node();
Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return ;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return ;
}
void printList(Node *node)
{
while (node != NULL)
{
cout<< " " <<node->data;
node = node->next;
}
}
int main()
{
Node* head = NULL;
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
cout << "Created Linked list is: " ;
printList(head);
return 0;
}
|
Output:
Created Linked list is: 1 7 8 6 4
Time complexity: O(N) where N is size of given linked list
Auxiliary space: O(1)
Please refer complete article on Linked List | Set 2 (Inserting a node) for more details!
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2023
Like Article
Save Article