Deletion in a Linked List in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Unlinking and Freeing a Node

To delete a node, unlink it by making its predecessor point to its successor, then free it. Deleting the head must update the head pointer. A neat technique is a pointer to a pointer (struct Node **pp), which treats the head and interior links the same way.

Remember
int delete_value(struct Node **head, int v) {
    for (struct Node **pp = head; *pp; pp = &(*pp)->next) {
        if ((*pp)->data == v) {
            struct Node *t = *pp;
            *pp = t->next;       /* unlink */
            free(t);
            return 1;
        }
    }
    return 0;
}

Cheat Sheet

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

TopicDetail
UnlinkPredecessor points to successor
FreeAfter unlinking, once
Head deletionUpdate head
Techniquestruct Node **pp

C Examples

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

Example 1 — Delete by value

Delete the head, a middle node, and a missing value.

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 int delete_value(struct Node **head, int v) {
    for (struct Node **pp = head; *pp; pp = &(*pp)->next) {
        if ((*pp)->data == v) {
            struct Node *t = *pp;
            *pp = t->next;
            free(t);
            return 1;
        }
    }
    return 0;
}

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

    delete_value(&head, 1);  print_list(head);
    delete_value(&head, 3);  print_list(head);
    if (!delete_value(&head, 9)) printf("9 not found\n");

    free_list(head);
    return 0;
}

How It Works

  1. pp points at the link that points to the current node.
  2. Rewriting *pp unlinks the node, even when it is the head.
  3. The function returns 0 when the value is absent.

Edge Cases & Pitfalls

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

uaf

Using a node after free

Do not read t->next after free(t).

leak

Unlinking without freeing

Forgetting free leaks the node.

Key Takeaways

  • Unlink, then free.
  • Pointer-to-pointer unifies head and middle.
  • Return a status.

One line: To delete a node, unlink it by making its predecessor point to its successor, then free it.

Frequently Asked Questions

Find its predecessor, set predecessor->next to node->next, then free the node. For the head, update the head pointer instead.
It lets one loop handle both the head and interior nodes without a special case.
Continue with "Searching a Linked List in C" next. Find a value in a linked list with a linear scan. The C Linked Lists hub lists every topic in order.

Next: Searching a Linked List in C

Find a value in a linked list with a linear scan.

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.

8 people found this page helpful