#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →

Single Linked List Implementation in C

A singly linked list is usually the first data structure where C students really have to use pointers and malloc(). Once you understand it, stacks, queues, trees and graphs become much easier.

Single Linked List Implementation in C

Singly Linked List in C: Insert, Delete, Search and Reverse (Full Program)

This guide builds a linked list step by step and ends with one complete program you can compile and run.

What is a singly linked list?

A singly linked list is a chain of nodes. Each node stores:

  1. the data (for example an integer), and
  2. a pointer to the next node.

The first node is called the head. The last node’s next pointer is NULL, which marks the end.

head -> [10 | •] -> [20 | •] -> [30 | NULL]

Unlike an array, nodes are not stored next to each other in memory. Each node can be anywhere; the pointers connect them.

Array vs linked list

Point Array Singly linked list
Size Fixed when created (for static arrays) Grows and shrinks at run time
Access to i-th element O(1) O(n) — must walk from head
Insert at beginning O(n) — shift elements O(1)
Extra memory None One pointer per node
Memory layout Continuous Scattered

Use a linked list when you insert and delete often and rarely need random access.

Step 1: The node structure

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

Step 2: Creating a node

struct Node *create_node(int value) {
    struct Node *n = malloc(sizeof(struct Node));
    if (n == NULL) {
        printf("Memory allocation failed\n");
        exit(1);
    }
    n->data = value;
    n->next = NULL;
    return n;
}

Always check if malloc() returned NULL. It rarely happens on a laptop, but it is a good habit and examiners like it.

Why we pass struct Node **head

Functions that may change the head (insert at beginning, delete first node, reverse) receive a pointer to the head pointer. If you pass struct Node *head instead, the function changes only its local copy and the list in main() never changes. This is the single most common bug in student code.

Step 3: Insert operations

Insert at the beginning, O(1) — the new node points to the old head, and becomes the new head.

void insert_at_beginning(struct Node **head, int value) {
    struct Node *n = create_node(value);
    n->next = *head;
    *head = n;
}

Insert at the end, O(n) — walk to the last node and attach the new node.

void insert_at_end(struct Node **head, int value) {
    struct Node *n = create_node(value);
    if (*head == NULL) {
        *head = n;
        return;
    }
    struct Node *temp = *head;
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = n;
}

Insert at a position (1 = first). If the position is larger than the list, the node is added at the end.

void insert_at_position(struct Node **head, int value, int pos) {
    if (pos <= 1 || *head == NULL) {
        insert_at_beginning(head, value);
        return;
    }
    struct Node *temp = *head;
    for (int i = 1; i < pos - 1 && temp->next != NULL; i++)
        temp = temp->next;
    struct Node *n = create_node(value);
    n->next = temp->next;
    temp->next = n;
}

The order of the last two lines matters: first connect the new node to the rest of the list, then connect the previous node to the new node. Doing it the other way round loses the rest of the list.

Step 4: Delete a node by value

void delete_value(struct Node **head, int value) {
    struct Node *temp = *head, *prev = NULL;
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("%d not found\n", value);
        return;
    }
    if (prev == NULL)
        *head = temp->next;      /* deleting the first node */
    else
        prev->next = temp->next;
    free(temp);
}

We keep a prev pointer because in a singly linked list you cannot go backwards.

Step 5: Search, count and display

int search(struct Node *head, int value) {
    int pos = 1;
    while (head != NULL) {
        if (head->data == value)
            return pos;
        head = head->next;
        pos++;
    }
    return -1;
}

int count_nodes(struct Node *head) {
    int c = 0;
    while (head != NULL) {
        c++;
        head = head->next;
    }
    return c;
}

void display(struct Node *head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

These functions don’t change the list, so a normal struct Node *head is enough.

Step 6: Reverse the list

Reversing is a favourite interview question. We walk through the list once and flip each next pointer.

void reverse(struct Node **head) {
    struct Node *prev = NULL, *curr = *head, *next = NULL;
    while (curr != NULL) {
        next = curr->next;   /* save the rest of the list */
        curr->next = prev;   /* flip the pointer */
        prev = curr;
        curr = next;
    }
    *head = prev;
}

Time O(n), extra space O(1).

Step 7: Free the memory

Every malloc() needs a free(). Before the program ends, release all nodes:

void free_list(struct Node **head) {
    struct Node *temp;
    while (*head != NULL) {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

Complete program

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

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

struct Node *create_node(int value) {
    struct Node *n = malloc(sizeof(struct Node));
    if (n == NULL) {
        printf("Memory allocation failed\n");
        exit(1);
    }
    n->data = value;
    n->next = NULL;
    return n;
}

void insert_at_beginning(struct Node **head, int value) {
    struct Node *n = create_node(value);
    n->next = *head;
    *head = n;
}

void insert_at_end(struct Node **head, int value) {
    struct Node *n = create_node(value);
    if (*head == NULL) {
        *head = n;
        return;
    }
    struct Node *temp = *head;
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = n;
}

void insert_at_position(struct Node **head, int value, int pos) {
    if (pos <= 1 || *head == NULL) {
        insert_at_beginning(head, value);
        return;
    }
    struct Node *temp = *head;
    for (int i = 1; i < pos - 1 && temp->next != NULL; i++)
        temp = temp->next;
    struct Node *n = create_node(value);
    n->next = temp->next;
    temp->next = n;
}

void delete_value(struct Node **head, int value) {
    struct Node *temp = *head, *prev = NULL;
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }
    if (temp == NULL) {
        printf("%d not found\n", value);
        return;
    }
    if (prev == NULL)
        *head = temp->next;
    else
        prev->next = temp->next;
    free(temp);
}

int search(struct Node *head, int value) {
    int pos = 1;
    while (head != NULL) {
        if (head->data == value)
            return pos;
        head = head->next;
        pos++;
    }
    return -1;
}

int count_nodes(struct Node *head) {
    int c = 0;
    while (head != NULL) {
        c++;
        head = head->next;
    }
    return c;
}

void reverse(struct Node **head) {
    struct Node *prev = NULL, *curr = *head, *next = NULL;
    while (curr != NULL) {
        next = curr->next;
        curr->next = prev;
        prev = curr;
        curr = next;
    }
    *head = prev;
}

void display(struct Node *head) {
    while (head != NULL) {
        printf("%d -> ", head->data);
        head = head->next;
    }
    printf("NULL\n");
}

void free_list(struct Node **head) {
    struct Node *temp;
    while (*head != NULL) {
        temp = *head;
        *head = (*head)->next;
        free(temp);
    }
}

int main(void) {
    struct Node *head = NULL;

    insert_at_end(&head, 20);
    insert_at_end(&head, 30);
    insert_at_beginning(&head, 10);
    insert_at_position(&head, 25, 3);
    printf("List: ");
    display(head);

    printf("Nodes: %d\n", count_nodes(head));
    printf("Position of 30: %d\n", search(head, 30));

    delete_value(&head, 25);
    printf("After deleting 25: ");
    display(head);
    delete_value(&head, 99);

    reverse(&head);
    printf("Reversed: ");
    display(head);

    free_list(&head);
    printf("After free: ");
    display(head);
    return 0;
}

Compile and run:

gcc -Wall -Wextra -o linkedlist linkedlist.c
./linkedlist

Output

List: 10 -> 20 -> 25 -> 30 -> NULL
Nodes: 4
Position of 30: 4
After deleting 25: 10 -> 20 -> 30 -> NULL
99 not found
Reversed: 30 -> 20 -> 10 -> NULL
After free: NULL

Time complexity summary

Operation Time
Insert at beginning O(1)
Insert at end (without tail pointer) O(n)
Insert at position O(n)
Delete by value O(n)
Search O(n)
Reverse O(n)

Tip: if you keep an extra tail pointer, insert at end also becomes O(1).

A typical “why is my list empty?” moment

Imagine a student who writes void insert(struct Node *head, int value), adds three numbers in main(), and then prints the list — only to see NULL. The function clearly runs; a printf inside it even shows the new node. So where did the nodes go?

They went into the function’s own copy of head. When the function ended, that copy disappeared, and main()’s head was still NULL (and the nodes became a memory leak). Changing the parameter to struct Node **head and calling insert(&head, 10) fixes it. Almost everyone who learns linked lists in C hits this once — and after fixing it, pointers to pointers finally make sense.

Common mistakes with linked lists in C

  • Passing head by value when the function must change it (see above).
  • Losing the rest of the list by changing temp->next before saving it.
  • Dereferencing NULL, e.g. temp->next when temp is already NULL. Check the loop condition carefully.
  • Using a node after free(). Once freed, never read it again.
  • Memory leaks — forgetting free_list(). On Linux you can check with valgrind --leak-check=full ./linkedlist.

Practice questions

  1. Find the middle node in one pass (hint: slow and fast pointers).
  2. Detect a loop in a linked list (Floyd’s cycle detection).
  3. Delete the n-th node from the end.
  4. Merge two sorted linked lists into one sorted list.
  5. Add a tail pointer and make insert at end O(1).

Frequently asked questions

What is the difference between singly and doubly linked list?

In a singly linked list each node points only to the next node. In a doubly linked list each node also points to the previous node, so you can move both ways, but each node uses more memory.

Why is NULL used at the end?

It tells the program where the list ends, so loops like while (temp != NULL) stop correctly.

Can a linked list be stored in an array?

Yes, using array indexes instead of pointers (a “static linked list”), but in C the usual way is with malloc() and pointers.

References

Placed Students

Our Clients

Partners

...