Insertion in a singly linked list refers to adding a new node at a specific position in the list. Each node in the list has two parts: the data and the pointer that stores the address of the next node. Unlike arrays, we do not shift elements to insert new data. Instead, we simply adjust the pointer links to include the new node. This makes insertion operations faster and more efficient, especially for large datasets.
Structure of Singly Linked List

Node
The basic part of a singly linked list is a node. Each node has two parts:
- Data: This holds the actual value, like a number, letter, or any type of information we want to store.
- Next: This is a pointer that tells where the next node is present. It connects nodes together, keeping the list in order.
Head
- The head is a special pointer that points to the first node of a singly linked list.
NULL
- NULL is a special value used in a linked list to show that a node does not point to any other node. In a singly linked list, the last node’s pointer is set to NULL, which means it is the end of the list.
Why Insertion is Needed at the Beginning and End ?
Insertion at the beginning of a linked list is used when we want to add a new node as the first element. This operation is very fast because we only create a new node, point its next pointer to the current head, and then update the head to this new node. It is useful when the new data needs to appear at the start of the list.
Insertion at the end of a linked list is used when we want to add a new node after all the existing nodes. This keeps the original order of the list unchanged while placing the new data at the last position. To perform this operation, we start from the head and traverse the list until we reach the final node. Then, we update the last node’s next pointer to link it with the new node.
Insert a node at the Beginning of Singly linked list
Algorithm to Insert a node at the Beginning
Step 1: Start
Begin the process by creating a new node that will be inserted into the list.
Step 2: Allocate Memory
Use malloc() to allocate memory for the new node.
Step 3: Check Memory Availability
If memory allocation fails (new_node == NULL), display “Memory not available” and stop the operation.
Step 4: Initialize the New Node
- Set the data field of the new node to the value you want to insert.
- Set the next pointer of the new node to
NULLtemporarily.
Step 5: Insert the New Node
- If the list is empty (
head == NULL):- Make the new node the head.
- If the list is not empty:
- Create a temporary pointer
tempand set it tohead. - Point the new node’s next pointer to the current head.
- Update head to the new node.
- Create a temporary pointer
Step 6: Display Success Message
Print “Node added successfully!” once insertion is complete.
Step 7: End

C Program to Insert a node at the Beginning of Singly linked list
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
struct node *newNode = (struct node *)malloc(sizeof(struct node));
return (newNode);
}
void insertNodeAtBegin(){
struct node *newNode, *temp;
newNode=createNode();
printf("Enter an element you want to insert at begining of the list:");
scanf("%d",&newNode->data);
if(head==NULL){
head=newNode;
}
else{
temp=head;
head=newNode;
head->next=temp;
}
}
void viewList(){
struct node* temp=head;
if(temp==NULL){
printf("list is empty");
}
else{
printf("Elements of Linked List: ");
while(temp->next!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d \t",temp->data);
}
}
int menu(){
int choice;
printf("\n 1. Insert at beginning of Linked List");
printf("\n 2. Travesre/View Linked List");
printf("\n 3. Exit");
printf("\n Please enter your choice: \t");
scanf("%d",&choice);
return(choice);
}
void main(){
while(1){
switch(menu()){
case 1:
insertNodeAtBegin();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("invalid choice");
}
getch();
}
}
Output
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at beginning of the list: 10
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at beginning of the list: 20
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at beginning of the list: 30
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 2
Elements of Linked List: 30 20 10
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 4
invalid choice
1. Insert at beginning of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 3
[Program terminates]
Explanation of the above Program
malloc()dynamically allocates memory for the new node.newNode->data = datastores the given value in the new node.newNode->next = headconnects the new node to the existing first node.head = newNodeupdates the HEAD pointer to the newly inserted node.
Time Complexity
| Case | Time Complexity | Reason |
|---|---|---|
| Best Case | O(1) | Creating a new node and updating pointers only; no traversal needed. |
| Worst Case | O(1) | Same steps even if the list is large; only pointer updates required. |
| Average Case | O(1) | Same steps for any insertion; independent of list size. |
Space Complexity
| Case | Space Complexity | Reason |
|---|---|---|
| Best Case | O(1) | Only one new node is allocated. |
| Worst Case | O(1) | Only one new node is allocated. |
| Average Case | O(1) | Only one new node is allocated. |
Insert a node at the end/tail of Singly linked list
Algorithm to Insert a node at the end
Step 1: Start
Start by creating a new node to add to the Singly Linked list.
Step 2: Allocate memory for the new node
Use malloc() to create space in memory for the new node.
Step 3: Check memory
If memory is not available (new node = NULL), print “Memory not available” and stop.
Step 4: Initialize the new node
- Set the data part of the new node to the value we want to insert.
- Set the next part of the new node to
NULL, because it will be the last node.
Step 5: Insert the new node
- If the list is empty (
head == NULL), make the new node the head. - If the list is not empty:
- Create a temporary pointer
tempand set it tohead. - Traverse the list until
temp → next == NULL(the last node). - Set
temp → nextto point to the new node.
- Create a temporary pointer
Step 6: Display success message
Print “Node added at the end successfully!”
Step 7: End
The new node is now added at the end of the linked list.

C Program to Insert a node at the end/tail of Singly linked list
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
// Structure of node
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
// Function to create a new node
struct node* createNode() {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->next = NULL; // Always initialize
return newNode;
}
// Insert node at end
void insertNodeAtEnd() {
struct node *newNode, *temp;
newNode = createNode();
printf("Enter an element you want to insert at end of the list: ");
scanf("%d", &newNode->data);
if (head == NULL) {
head = newNode; // First node
} else {
temp = head;
while (temp->next != NULL) { // Traverse till last node
temp = temp->next;
}
temp->next = newNode; // Link last node to newNode
}
printf("Node inserted at end successfully!\n");
}
// Traverse or display list
void viewList() {
struct node *temp = head;
if (temp == NULL) {
printf("List is empty\n");
} else {
printf("Elements of Linked List: ");
while (temp != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}
// Menu function
int menu() {
int choice;
printf("\n1. Insert at end of Linked List");
printf("\n2. Traverse/View Linked List");
printf("\n3. Exit");
printf("\nPlease enter your choice:\t");
scanf("%d", &choice);
return choice;
}
// Driver code
void main() {
while (1) {
switch (menu()) {
case 1:
insertNodeAtEnd();
break;
case 2:
viewList();
break;
case 3:
exit(0);
default:
printf("Invalid choice\n");
}
getch();
}
}
Output
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at end of the list: 10
Node inserted at end successfully!
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at end of the list: 20
Node inserted at end successfully!
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 1
Enter an element you want to insert at end of the list: 30
Node inserted at end successfully!
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 2
Elements of Linked List: 10 20 30
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 4
Invalid choice
1. Insert at end of Linked List
2. Traverse/View Linked List
3. Exit
Please enter your choice: 3
[Program terminates]
Explanation of the Program
malloc()allocates memory for the new node.newNode->data = datastores the given value.newNode->next = NULLmarks the new node as the last node.if (head == NULL)checks whether the linked list is empty. If it is empty, the new node becomes the first node.temp = headstarts traversal from the first node.while (temp->next != NULL)moves the temporary pointer until it reaches the last node.temp->next = newNodeconnects the existing last node to the newly created node.
Time Complexity
| Case | Time Complexity | Reason |
|---|---|---|
| Best Case | O(n) | Even if the list has one node or is empty, we may need to traverse to find the last node (except if the list is empty, then O(1)). |
| Worst Case | O(n) | When the list has n nodes, traversal through all nodes is required to reach the last node. |
| Average Case | O(n) | On average, traversal is required through n/2 nodes, but in Big-O notation, it is O(n). |
Space Complexity
| Case | Space Complexity | Reason |
|---|---|---|
| Best Case | O(1) | Only one new node is allocated. |
| Worst Case | O(1) | Only one new node is allocated. |
| Average Case | O(1) | Only one new node is allocated. |