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.
public class Node {
public int Value;
public Node Next;
}
public bool Search(Node head, int target) {
Node current = head;
while (current != null) {
if (current.Value == target) return true;
current = current.Next;
}
return false;
}
This code checks each node in the linked list one by one to find the target value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Checking each node's value in the linked list.
- 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 search time 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 may need to check every node, so the time grows with list size, making it slower for longer lists.
Understanding this helps you explain how simple searches work and why data structure choice matters for speed.
"What if the linked list was sorted? How would that affect 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 target value.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 the value exists in the list -> Option BQuick Check:
Search = Find value presence [OK]
- Confusing search with insertion or deletion
- Thinking search changes the list
- Assuming search returns the node position always
head?Solution
Step 1: Understand variable assignment syntax in C#
To assign a reference, use single equals sign=.Step 2: Check each option for correct assignment
Node current = head; uses=, which is correct. Others use invalid operators.Final Answer:
Node current = head; -> Option CQuick Check:
Assignment uses = in C# [OK]
- Using == instead of = for assignment
- Using invalid operators like => or <-
- Confusing assignment with comparison
class Node {
public int data;
public Node next;
public Node(int d) { data = d; next = null; }
}
Node head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
Node current = head;
bool found = false;
while (current != null) {
if (current.data == 10) {
found = true;
break;
}
current = current.next;
}
Console.WriteLine(found);Solution
Step 1: Trace the linked list nodes and search loop
The list has nodes with data 5 -> 10 -> 15. The loop checks each node's data for 10.Step 2: Check when the value 10 is found
At second node, data is 10, so found becomes true and loop breaks.Final Answer:
True -> Option DQuick Check:
Value 10 found = True [OK]
- Forgetting to break loop after finding value
- Printing current.data instead of found
- Null pointer errors if next not checked
Node current = head;
while (current.next != null) {
if (current.data == 20) {
Console.WriteLine("Found");
break;
}
current = current.next;
}Solution
Step 1: Analyze the loop condition
The loop runs whilecurrent.next != null, so it stops before last node.Step 2: Understand impact on search
The last node's data is never checked, so search may miss the value if it's there.Final Answer:
It skips checking the last node -> Option AQuick Check:
Loop condition misses last node [OK]
- Using current.next != null instead of current != null
- Assuming last node is checked automatically
- Not handling empty list (head null)
x instead of just checking presence?Solution
Step 1: Understand the need to count all occurrences
Since duplicates exist, we must check every node to count all matches.Step 2: Traverse entire list and count matches
Loop through each node, increment a counter when node.data equals x, then return the count.Final Answer:
Traverse all nodes, increment count when node.data == x, then return count -> Option AQuick Check:
Count all matches by full traversal [OK]
- Stopping search after first match
- Using recursion incorrectly for counting
- Modifying list during search unintentionally
Ready to prove it?
Pass the test and this pattern moves unknown
