Search for a Value in Linked List in DSA Java - Time & Space Complexity
When searching for a value in a linked list, we want to know how the time it takes grows as the list gets longer.
We ask: How many steps does it take to find the value or decide it's not there?
Analyze the time complexity of the following code snippet.
public boolean search(Node head, int target) {
Node current = head;
while (current != null) {
if (current.data == 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 data against the target value.
- How many times: Up to once per node, until the target is found or list ends.
As the list gets longer, the number of checks grows roughly the same as the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 checks |
| 100 | Up to 100 checks |
| 1000 | Up to 1000 checks |
Pattern observation: The steps increase directly with the number of nodes.
Time Complexity: O(n)
This means the time to search grows linearly with the list size; doubling the list roughly doubles the work.
[X] Wrong: "Searching a linked list is always fast because you just follow the links."
[OK] Correct: You must check nodes one by one, so longer lists take more time, unlike arrays where you can jump to the middle.
Understanding this linear search helps you explain why linked lists are good for some tasks but not for quick searches, showing your grasp of data structure trade-offs.
"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 is present or not in the linked list.Final Answer:
To find if the value exists in the list -> Option CQuick Check:
Search means find value presence = To find if the value exists in the list [OK]
- Confusing search with insertion or deletion
- Thinking search changes the list
- Assuming search reverses the list
target?Solution
Step 1: Initialize traversal from head node
Traversal must start from the head node to check all nodes including the first.Step 2: Loop until current is null
Loop continues while current is not null to cover all nodes.Final Answer:
Node current = head; while(current != null) { if(current.data == target) break; current = current.next; } -> Option AQuick Check:
Start at head and loop till null = Node current = head; while(current != null) { if(current.data == target) break; current = current.next; } [OK]
- Starting traversal from null or head.next
- Looping until current.next is null misses last node
- Using wrong loop condition causing errors
Node current = head;
boolean found = false;
while(current != null) {
if(current.data == 5) {
found = true;
break;
}
current = current.next;
}
System.out.println(found);Solution
Step 1: Traverse nodes checking for value 5
Nodes checked in order: 3 (no), 5 (yes), loop breaks and found = true.Step 2: Print the found variable
Since value 5 is found, found is true, so output is true.Final Answer:
true -> Option DQuick Check:
Value 5 found = true [OK]
- Assuming output is node data instead of boolean
- Thinking loop misses the value 5
- Confusing true/false output
Node current = head;
while(current.next != null) {
if(current.data == target) {
System.out.println("Found");
break;
}
current = current.next;
}Solution
Step 1: Analyze loop condition
The loop runs while current.next != null, so it stops before the last node.Step 2: Check if last node is checked
Since last node's next is null, last node is never checked for target value.Final Answer:
It misses checking the last node -> Option BQuick Check:
Loop condition excludes last node = misses last node check [OK]
- Using current.next != null instead of current != null
- Assuming last node is checked
- Ignoring possibility of missing target at last node
Solution
Step 1: Traverse from head with index counter
Start at head, keep an index starting at 0, check each node's data.Step 2: Return index if value matches, else -1 after full traversal
If node data equals target, return current index; if not found after loop, return -1.Final Answer:
Traverse from head, increment index each step, return index if found, else -1 after loop -> Option AQuick Check:
Index search = traverse + count + return index or -1 [OK]
- Returning node data instead of index
- Traversing from tail in singly linked list (not possible)
- Incrementing index only on match causing wrong count
Ready to prove it?
Pass the test and this pattern moves unknown
