Searching a Linked List in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Linear Search in a List

Searching a singly linked list is a linear scan: walk from the head until the value matches or the list ends. It is O(n). Return a pointer to the node (or NULL when absent) so the caller can read or change the node, and compute the index with a counter if needed.

Remember
struct Node *find(struct Node *head, int key) {
    for (struct Node *p = head; p; p = p->next)
        if (p->data == key) return p;
    return NULL;
}

Cheat Sheet

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

TopicDetail
TimeO(n)
ReturnNode pointer or NULL
IndexCount while walking
Sorted listCan stop early

C Examples

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

Example 1 — Find a value and its index

Return NULL for missing values.

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 *find(struct Node *head, int key, int *index) {
    int i = 0;
    for (struct Node *p = head; p; p = p->next, i++) {
        if (p->data == key) {
            *index = i;
            return p;
        }
    }
    return NULL;
}

int main(void) {
    struct Node *head = new_node(4, new_node(8, new_node(15, new_node(16, NULL))));
    int idx;

    struct Node *hit = find(head, 15, &idx);
    if (hit) printf("found %d at index %d\n", hit->data, idx);
    if (!find(head, 7, &idx)) printf("7 not found\n");

    free_list(head);
    return 0;
}

How It Works

  1. The loop increments i alongside the pointer.
  2. A hit returns the node and sets the index through a pointer.
  3. A miss returns NULL, which the caller must check.

Edge Cases & Pitfalls

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

null

Unchecked result

Dereferencing the result without a NULL check crashes when the value is missing.

dup

Duplicates

The scan returns the first match only.

Key Takeaways

  • Linear scan.
  • Return the node.
  • Check for NULL.

One line: Searching a singly linked list is a linear scan: walk from the head until the value matches or the list ends.

Frequently Asked Questions

O(n) in the worst case because each node is visited in order.
Not efficiently: you cannot jump to the middle in O(1). Use an array or a balanced tree for fast searching.
Continue with "C Reversing a Linked List" next. Reverse the list in place with three pointers. The C Linked Lists hub lists every topic in order.

Next: Reversing a Linked List

Reverse the list in place with three pointers.

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.

9 people found this page helpful