Search for a Value in Linked List in DSA C++ - Time & Space Complexity
We want to understand how long it takes to find a value in a linked list as the list grows.
How does the search time change when the list gets bigger?
Analyze the time complexity of the following code snippet.
struct Node {
int data;
Node* next;
};
bool search(Node* head, int target) {
Node* current = head;
while (current != nullptr) {
if (current->data == target) return true;
current = current->next;
}
return false;
}
This code looks through each node in the linked list to find if the target value exists.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each node's data one by one.
- How many times: Up to once for every node in the list, until the target is found or list ends.
As the list gets longer, the search might 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 time to search grows linearly with the number of nodes in the list.
[X] Wrong: "Searching a linked list is always fast because you just follow the links."
[OK] Correct: You might have to check every node one by one, so it can take longer as the list grows.
Understanding this helps you explain how simple searches work and why linked lists have different performance than arrays.
"What if the linked list was sorted? How would the time complexity of searching change?"
Practice
Solution
Step 1: Understand the search operation
Searching in a linked list 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 list, not to modify the list.Final Answer:
To find if a specific value exists in the list -> Option BQuick Check:
Search means find value presence = To find if a specific value exists in the list [OK]
- Confusing search with insertion or deletion
- Thinking search changes the list
- Assuming search reverses the list
Solution
Step 1: Understand pointer usage in linked lists
In C++, linked list nodes are accessed via pointers, so we use a pointer variable to traverse.Step 2: Identify correct pointer initialization
We set a pointer 'current' to point to the head node to start traversal: Node* current = head;Final Answer:
Node* current = head; -> Option DQuick Check:
Use pointer to start traversal = Node* current = head; [OK]
- Using object instead of pointer for traversal
- Creating a new node instead of using existing head
- Not initializing traversal pointer correctly
struct Node {
int data;
Node* next;
};
Node* head = new Node{10, nullptr};
head->next = new Node{20, nullptr};
head->next->next = new Node{30, nullptr};
Node* current = head;
int searchValue = 20;
bool found = false;
while (current != nullptr) {
if (current->data == searchValue) {
found = true;
break;
}
current = current->next;
}
if (found) std::cout << "Value found";
else std::cout << "Value not found";Solution
Step 1: Trace the linked list nodes and values
The list has nodes with values 10 -> 20 -> 30. We search for 20.Step 2: Check the search loop logic
The loop checks each node's data. When it reaches node with 20, found becomes true and loop breaks.Final Answer:
Value found -> Option CQuick Check:
20 exists in list, so output = "Value found" [OK]
- Forgetting to update current pointer
- Using wrong comparison operator
- Not breaking loop after finding value
Node* current = head;
int searchValue = 15;
while (current->next != nullptr) {
if (current->data == searchValue) {
std::cout << "Found";
break;
}
current = current->next;
}
if (current == nullptr) std::cout << "Not Found";Solution
Step 1: Analyze the loop condition
The loop runs while current->next is not nullptr, so it stops before the last node.Step 2: Understand impact on search
The last node's data is never checked, so if the value is there, it will be missed.Final Answer:
The loop condition skips checking the last node -> Option AQuick Check:
Loop must check all nodes including last [OK]
- Using current->next != nullptr instead of current != nullptr
- Not checking last node's data
- Incorrectly handling nullptr checks
int searchPosition(Node* head, int value) {
int pos = 1;
Node* current = head;
while (current != nullptr) {
if (current->data == value) return pos;
current = current->next;
pos++;
}
return -1;
}Solution
Step 1: Understand the function logic
The function starts position at 1 and traverses nodes, checking data against value.Step 2: Check return values
If value matches, it returns current position; if not found, returns -1.Final Answer:
Returns the position of value or -1 if not found -> Option AQuick Check:
Function returns index or -1 if missing [OK]
- Returning boolean instead of position
- Starting position count at 0 instead of 1
- Not returning -1 when value not found
Ready to prove it?
Pass the test and this pattern moves unknown
