Insertion in a Linked List in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Where to Insert a Node

Insertion changes a few pointers. Head insertion is O(1): n->next = head; head = n;. Tail insertion needs a walk (or a tail pointer). Insertion at index i walks to node i - 1 and splices the new node in. When splicing, set the new node's next before changing the previous node's next.

Remember
n->next = prev->next;   /* 1. new node points ahead */
prev->next = n;         /* 2. previous points to new */

Cheat Sheet

Keep these facts handy while you work with Insertion in a Linked List in C in C.

TopicDetail
HeadO(1)
TailO(n) without a tail pointer
At indexO(n) walk, O(1) splice
OrderSet n->next first

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Deletion in a Linked List in C.

Example 1 — Insert at head, tail, and position

Start from 1, 2, 3.

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

struct Node {
    int data;
    struct Node *next;
};

static struct Node *new_node(int value, struct Node *next) {
    struct Node *n = malloc(sizeof *n);
    if (!n) exit(1);
    n->data = value;
    n->next = next;
    return n;
}

static void print_list(const struct Node *p) {
    for (; p; p = p->next) printf("%d%s", p->data, p->next ? " " : "");
    printf("\n");
}

static void free_list(struct Node *p) {
    while (p) {
        struct Node *t = p->next;
        free(p);
        p = t;
    }
}

static struct Node *insert_tail(struct Node *head, int v) {
    if (!head) return new_node(v, NULL);
    struct Node *p = head;
    while (p->next) p = p->next;
    p->next = new_node(v, NULL);
    return head;
}

static struct Node *insert_at(struct Node *head, int idx, int v) {
    if (idx == 0) return new_node(v, head);
    struct Node *p = head;
    for (int i = 0; p && i < idx - 1; i++) p = p->next;
    if (p) p->next = new_node(v, p->next);
    return head;
}

int main(void) {
    struct Node *head = new_node(1, new_node(2, new_node(3, NULL)));

    head = new_node(0, head);        print_list(head);
    head = insert_tail(head, 4);     print_list(head);
    head = insert_at(head, 2, 99);   print_list(head);

    free_list(head);
    return 0;
}

How It Works

  1. Head insertion makes the new node point at the old head.
  2. Tail insertion walks to the last node and links the new one.
  3. At index 2 the code stops at node index 1 and splices in 99.

Edge Cases & Pitfalls

Check these before relying on Insertion in a Linked List in C in real programs.

order

Wrong pointer order

Setting prev->next = n first loses the rest of the list. Link the new node forward first.

range

Index past the end

Check that the walk did not reach NULL before dereferencing.

Key Takeaways

  • Forward link first.
  • Head is the fast path.
  • Return the new head.

One line: Insertion changes a few pointers.

Frequently Asked Questions

It only changes two pointers and needs no traversal or shifting.
Walk to the node before the insertion point, set the new node's next to that node's next, then set that node's next to the new node.
Continue with "Deletion in a Linked List in C" next. Remove nodes safely by head, value, or position and free them. The C Linked Lists hub lists every topic in order.

Next: Deletion in a Linked List in C

Remove nodes safely by head, value, or position and free them.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful