Bird
0
0
DSA Cprogramming

Search for a Value in Linked List in DSA C

Choose your learning style9 modes available
Mental Model
We look at each item in the list one by one until we find the value or reach the end.
Analogy: Like flipping through pages of a book to find a specific word, checking each page in order.
head -> [1] -> [2] -> [3] -> null
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, search for value 2
Goal: Find if the value 2 exists in the list and where
Step 1: Start at the first node with value 1
head -> [1 ↑] -> [2] -> [3] -> null
Why: We begin searching from the start of the list
Step 2: Check if current node value 1 equals 2 (it does not), move to next node
head -> [1] -> [2 ↑] -> [3] -> null
Why: Value not found yet, so we continue to next node
Step 3: Check if current node value 2 equals 2 (it does), stop search
head -> [1] -> [2 ↑] -> [3] -> null
Why: Found the value we are searching for
Result:
head -> [1] -> [2 ↑] -> [3] -> null
Value 2 found in the list
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure for linked list
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* create_node(int value) {
    Node* new_node = (Node*)malloc(sizeof(Node));
    new_node->data = value;
    new_node->next = NULL;
    return new_node;
}

// Function to search for a value in the linked list
int search_value(Node* head, int target) {
    Node* current = head; // start at head
    while (current != NULL) { // while not end of list
        if (current->data == target) {
            return 1; // found the value
        }
        current = current->next; // move to next node
    }
    return 0; // value not found
}

// Function to print the linked list
void print_list(Node* head, int highlight) {
    Node* current = head;
    while (current != NULL) {
        if (current->data == highlight) {
            printf("[%d ↑] -> ", current->data); // highlight found node
        } else {
            printf("[%d] -> ", current->data);
        }
        current = current->next;
    }
    printf("null\n");
}

int main() {
    // Create linked list 1 -> 2 -> 3 -> null
    Node* head = create_node(1);
    head->next = create_node(2);
    head->next->next = create_node(3);

    int target = 2;
    int found = search_value(head, target);

    print_list(head, found ? target : -1);
    if (found) {
        printf("Value %d found in the list\n", target);
    } else {
        printf("Value %d not found in the list\n", target);
    }

    // Free memory
    Node* current = head;
    while (current != NULL) {
        Node* temp = current;
        current = current->next;
        free(temp);
    }

    return 0;
}
Node* current = head; // start at head
initialize current pointer to start of list
while (current != NULL) { // while not end of list
loop through nodes until end
if (current->data == target) {
check if current node has target value
return 1; // found the value
stop search and report found
current = current->next; // move to next node
advance to next node to continue search
OutputSuccess
[1] -> [2 ↑] -> [3] -> null Value 2 found in the list
Complexity Analysis
Time: O(n) because in worst case we check each node once until we find the value or reach the end
Space: O(1) because we only use a few variables regardless of list size
vs Alternative: Compared to arrays, linked list search is similar O(n), but linked lists do not allow direct access so traversal is necessary
Edge Cases
Empty list (head is NULL)
Search immediately returns not found
DSA C
while (current != NULL) {
Value not in list
Search traverses entire list and returns not found
DSA C
while (current != NULL) {
Value at head node
Search finds value immediately and returns found
DSA C
if (current->data == target) {
When to Use This Pattern
When you need to find if a value exists in a linked list, use linear search by checking each node one by one.
Common Mistakes
Mistake: Not checking if current node is NULL before accessing data
Fix: Always check current != NULL in the loop condition before accessing current->data
Summary
Searches through a linked list to find if a value exists.
Use when you need to check presence of a value in a linked list.
The key is to check each node one by one until you find the value or reach the end.