×

DS - Basics

DS - Array

DS - Linked List

DS - Stack

DS - Queue

DS Hashing

DS Tree

DS - Graph

DS Programs Using C/C++

DS - Miscellaneous Topics

Single Linked List Insertion

Last Updated : November 24, 2025

Insertion in a singly linked list means adding a new node at the beginning, at the end, or at any specific position in the list. Each case follows simple pointer adjustments to maintain the proper link structure.

All Possible Cases

  • Inserting at the beginning
  • Inserting at the end
  • Inserting at a given position

Insertion Algorithms

1. Inserting at the Beginning

In this method, the new node is added before the current head node. After insertion, the new node becomes the head of the list. Follow these steps:

  1. Set the next pointer of the new node to the current head.
  2. Update the head pointer to the new node.

2. Inserting at the End

Here, the new node becomes the last node of the list. Its next pointer will always be NULL. Follow these steps:

  1. Set the next pointer of the new node to NULL.
  2. Traverse the list to reach the last node and set its next pointer to the new node.

3. Inserting at a Given Position

This method inserts the new node at any specific position inside the list. Follow the steps:

  1. Traverse the list to reach the node just before the target position.
  2. Store the current node’s next pointer in a temporary pointer (e.g., tmp_node).
  3. Set the next pointer of the new node to tmp_node and update the current node’s next pointer to the new node.

After these steps, the new node is successfully inserted into the singly linked list.

C Program to Insert a New Node into a Single Linked List

//
//  main.c
//  linkedlist_insert_element_code
//
//  Created by Anshuman Singh on 22/06/19.
//  Copyright © 2019 Anshuman Singh. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>

typedef struct node {
    int data;
    struct node* next;
} node;

void insert_node(node** head, int val, int position);

void insert_node(node** head, int val, int position)
{
    struct node *curr = *head, *tmp_node = NULL;
    int count = 1;

    tmp_node = (node*)malloc(sizeof(node));

    if (tmp_node == NULL) {
        printf("Memory allocation is failed:");
        return;
    }
    tmp_node->data = val;
    tmp_node->next = NULL;

    if (*head == NULL) {
        // List is empty, assigning head pointer to tmp_node
        *head = tmp_node;
        return;
    }
    if (position == 1) {
        // Inserting node at the beginning of the list
        tmp_node->next = *head;
        *head = tmp_node;
        return;
    }
    while (curr && count < position - 1) {
        curr = curr->next;
        count++;
    }
    if (position > (count + 1)) {
        printf("\n position doesn't exists in the list ");
        return;
    }
    if (count + 1 == position && curr->next == NULL) {
        // Inseting node at the end of the list
        curr->next = tmp_node;
        return;
    }
    // Inserting node in the list at given position
    tmp_node->next = curr->next;
    curr->next = tmp_node;
}

void print_list(node* head)
{
    printf("\nList elements:\n");
    while (head) {
        printf("%d ", head->data);
        head = head->next;
    }
    printf("\n");
    return;
}

int main()
{
    int num_nodes, value, index, position;
    node* head = NULL;

    printf("Enter the no. of nodes to create list: ");
    scanf("%d", &num_nodes);

    for (index = 1; index <= num_nodes; index++) {
        printf("Enter node data for position %d in the list:  ", index);
        scanf("%d", &value);
        insert_node(&head, value, index);
    }
    print_list(head);

    printf("\nInsert the element at 1st position:  ");
    scanf("%d", &value);
    insert_node(&head, value, 1);
    // We have inserted one more element, hence num_nodes get increased by 1
    num_nodes += 1;
    print_list(head);

    printf("\nInsert the element at last position:  ");
    scanf("%d", &value);
    insert_node(&head, value, num_nodes + 1);
    // We have inserted one more element, hence num_nodes will get increased by 1
    num_nodes += 1;
    print_list(head);

    printf("\nInsert the element at any position in the list\n");
    printf("Enter the position: ");
    scanf("%d", &position);
    printf("Enter the element value: ");
    scanf("%d", &value);
    insert_node(&head, value, position);
    // We have inserted one more element, hence num_nodes will get increased by 1
    num_nodes += 1;
    print_list(head);

    return 0;
}

Output

Enter the no. of nodes to create list: 5 
Enter node data for position 1 in the list:  11  
Enter node data for position 2 in the list:  22  
Enter node data for position 3 in the list:  33  
Enter node data for position 4 in the list:  44  
Enter node data for position 5 in the list:  55  
 
List elements:   
11 22 33 44 55   
 
Insert the element at 1st position:  10  
 
List elements:   
10 11 22 33 44 55
 
Insert the element at last position:  20 
 
List elements:   
10 11 22 33 44 55 20 
 
Insert the element at any position in the list   
Enter the position: 4
Enter the element value: 40  
 
List elements:   
10 11 22 40 33 44 55 20

Time and Space Complexity of Linked List Insertion

Inserting at the beginning: O(1)

Inserting at the end: O(n) — requires traversal

Inserting at a given position: O(n) — depends on position

Space Complexity: O(1) — no extra space used

Common Mistakes While Inserting in a Linked List

  • Forgetting to update the head pointer when inserting at the beginning.
  • Not setting the last node’s next pointer to NULL.
  • Incorrectly handling positions beyond the list length.
  • Missing temporary pointer and losing access to the rest of the list.

When Should You Use a Linked List?

  • When frequent insertions and deletions are required.
  • When memory allocation should be dynamic.
  • When shifting elements (as in arrays) is expensive.

FAQs

1. Is insertion faster in linked list or array?

Insertion is generally faster in a linked list because no shifting is required.

2. Can we insert at any position in a singly linked list?

Yes, but you must traverse the list to reach that position.

3. Which pointer is used for insertion?

Typically the next pointer of the previous node.

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.