Search for a Value in Linked List in DSA Go - 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 time to find a value change when the list gets bigger?
Analyze the time complexity of the following code snippet.
func Search(head *Node, target int) bool {
current := head
for current != nil {
if current.value == 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 value one by one.
- How many times: Up to once per node, until the target is found or list ends.
As the list gets longer, the search takes longer because it 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 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 may need to check every node, so time grows with list size, making it slower for long lists.
Understanding this helps you explain how linked lists work and why searching them takes time proportional to their length.
"What if the linked list was sorted? How would that change the time complexity of searching?"
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 a specific value exists in the list -> Option DQuick 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 structure
- Assuming search reverses the list
val?Solution
Step 1: Identify correct equality check syntax in Go
Use double equals (==) to compare values, not single equals (=) or :=.Step 2: Check the field name and comparison logic
Assuming node field is 'value', 'current.value == val' correctly compares values.Final Answer:
if current.value == val { return true } -> Option BQuick Check:
Use '==' for comparison in Go [OK]
- Using '=' instead of '==' for comparison
- Using ':=' inside if condition incorrectly
- Checking inequality instead of equality
type Node struct {
value int
next *Node
}
func search(head *Node, val int) bool {
current := head
for current != nil {
if current.value == val {
return true
}
current = current.next
}
return false
}
// List: 1 -> 3 -> 5 -> 7 -> nil
head := &Node{1, &Node{3, &Node{5, &Node{7, nil}}}}
fmt.Println(search(head, 5))Solution
Step 1: Trace the search function through the list
It checks nodes 1, 3, then 5. When it finds 5, it returns true immediately.Step 2: Confirm no errors occur
Code is correct and will print true because 5 exists in the list.Final Answer:
true -> Option AQuick Check:
Value 5 found in list = true [OK]
- Assuming search returns false if value is in list
- Thinking code causes panic due to nil pointer
- Confusing output with string instead of boolean
func search(head *Node, val int) bool {
current := head
for current.next != nil {
if current.value == val {
return true
}
current = current.next
}
return false
}Solution
Step 1: Analyze the loop condition
The loop runs while current.next != nil, so it stops before last node.Step 2: Understand impact on search
The last node's value is never checked, so search can miss the value if it's there.Final Answer:
Loop condition misses checking the last node -> Option AQuick Check:
Loop must check all nodes including last [OK]
- Using current.next != nil instead of current != nil
- Forgetting to check last node's value
- Misreading loop condition as correct
val instead of just true/false?Choose the correct approach.
Solution
Step 1: Understand requirement to count all occurrences
We must check every node because duplicates can appear anywhere.Step 2: Modify search to count matches
Traverse all nodes, increase count each time node.value equals val, then return count.Final Answer:
Traverse all nodes, increment count when node.value == val, return count -> Option CQuick Check:
Count all matches by full traversal [OK]
- Stopping search after first match
- Returning boolean instead of count
- Ignoring duplicates in list
Ready to prove it?
Pass the test and this pattern moves unknown
