Home C Linked Lists Searching a Linked List in C Searching a Linked List in C Definition
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.
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;
} Reference
Cheat Sheet Keep these facts handy while you work with Searching a Linked List in C in C.
Topic Detail Time O(n) Return Node pointer or NULL Index Count while walking Sorted list Can stop early
Code
C Examples Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Reversing a Linked List .
1. Find a value and its index Example 1 — Find a value and its index Return NULL for missing values.
#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;
} found 15 at index 2
7 not found How It Works The loop increments i alongside the pointer. A hit returns the node and sets the index through a pointer. 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.
Remember
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 What is the time complexity of searching a linked list? O(n) in the worst case because each node is visited in order.
Can I use binary search on a linked list? Not efficiently: you cannot jump to the middle in O(1). Use an array or a balanced tree for fast searching.
What should I learn after Searching a Linked List in C? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion