Search for a Value in Linked List in DSA C - Time & Space Complexity
When searching for a value in a linked list, we want to know how long it takes as the list grows.
We ask: How does the search time change when the list gets bigger?
Analyze the time complexity of the following code snippet.
// Search for a value in a linked list
int search(Node* head, int target) {
Node* current = head;
while (current != NULL) {
if (current->data == target) {
return 1; // Found
}
current = current->next;
}
return 0; // Not found
}
This code checks each node one by one until it finds the target or reaches the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each node's data once.
- How many times: Up to n times, where n is the number of nodes in the list.
As the list grows, the search may check more nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The number of checks grows directly with the list size.
Time Complexity: O(n)
This means the search time grows linearly with the number of nodes in the list.
[X] Wrong: "Searching a linked list is always fast because it's simple."
[OK] Correct: Even though the code is simple, the search may need to check every node, so it can take longer as the list grows.
Understanding this helps you explain how linked lists work and why searching them takes time proportional to their size.
"What if the linked list was sorted? How would that change the time complexity of searching for a value?"
Practice
Solution
Step 1: Understand the search operation
Searching means checking each node to see if it contains the value we want.Step 2: Identify the goal of searching
The goal is to find whether the value exists anywhere in the linked list.Final Answer:
To find if a specific value exists in the list -> Option AQuick Check:
Search = Find value in list [OK]
- Confusing search with insertion or deletion
- Thinking search changes the list
- Assuming search reverses the list
Solution
Step 1: Initialize pointer to head
We start searching from the head node, so current = head is correct.Step 2: Traverse until end or value found
Loop continues while current is not NULL, checking each node's data.Final Answer:
Node* current = head; while(current != NULL) { if(current->data == value) break; current = current->next; } -> Option AQuick Check:
Start at head and loop till NULL [OK]
- Initializing current to NULL instead of head
- Looping until current->next is NULL misses last node
- Using for loop with value as count incorrectly
typedef struct Node {
int data;
struct Node* next;
} Node;
int search(Node* head, int value) {
Node* current = head;
while(current != NULL) {
if(current->data == value) {
return 1;
}
current = current->next;
}
return 0;
}
int main() {
Node n3 = {10, NULL};
Node n2 = {7, &n3};
Node n1 = {3, &n2};
int found = search(&n1, 7);
printf("%d", found);
return 0;
}Solution
Step 1: Trace the search function
The function checks each node's data: 3 (no), 7 (yes), so it returns 1 immediately.Step 2: Understand main output
Main prints the returned value, which is 1, indicating value found.Final Answer:
1 -> Option DQuick Check:
Value found = 1 [OK]
- Expecting the value itself to be printed
- Confusing return 1 with return 0
- Thinking code causes compilation error
int search(Node* head, int value) {
Node* current = head;
while(current->next != NULL) {
if(current->data == value) {
return 1;
}
current = current->next;
}
return 0;
}Solution
Step 1: Analyze loop condition
The loop runs while current->next != NULL, so it stops before last node.Step 2: Consequence of condition
The last node's data is never checked, so search may miss value at last node.Final Answer:
The loop condition misses checking the last node -> Option CQuick Check:
Loop condition excludes last node [OK]
- Using current->next != NULL instead of current != NULL
- Assuming last node is checked automatically
- Confusing return types
Solution
Step 1: Understand position counting
Positions start at 1 for head node, increment by 1 for each next node.Step 2: Locate value 12 in list
Values: 5 (pos 1), 8 (pos 2), 12 (pos 3), so 12 is at position 3.Final Answer:
3 -> Option BQuick Check:
Value 12 is third node [OK]
- Returning zero-based index instead of 1-based
- Returning -1 even if value exists
- Confusing position with value
Ready to prove it?
Pass the test and this pattern moves unknown
