Search for a Value in Linked List in DSA Python - Time & Space Complexity
When searching for a value in a linked list, we want to know how the time to find it changes as the list grows.
We ask: How many steps does it take to find a value when the list gets bigger?
Analyze the time complexity of the following code snippet.
class Node:
def __init__(self, value):
self.value = value
self.next = None
def search_linked_list(head, target):
current = head
while current is not None:
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 for every node in the list until the target is found or the 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 grow directly with the size of the list.
Time Complexity: O(n)
This means the time to find a value grows in a straight line with the number of nodes in the list.
[X] Wrong: "Searching a linked list is always very fast because you just follow the links."
[OK] Correct: You must check nodes one by one, so if the list is long, it can take a long time.
Understanding this helps you explain how simple searches work and why linked lists have different speed traits than arrays.
"What if the linked list was sorted? How would that change the time complexity of searching for a value?"
Practice
Solution
Step 1: Understand linked list traversal
Linked lists do not have indexes, so we must check nodes one by one from the start.Step 2: Search each node for the value
We compare the target value with each node's data until we find a match or reach the end.Final Answer:
Checking each node one by one until the value is found or list ends -> Option CQuick Check:
Search = Sequential check [OK]
- Assuming direct access by index
- Trying to jump to middle node without traversal
- Sorting before searching in linked list
head?Solution
Step 1: Identify the starting node
Search starts from the first node, which is referenced byhead.Step 2: Assign current pointer correctly
Setcurrent = headto begin traversal from the first node.Final Answer:
current = head -> Option BQuick Check:
Start at head node [OK]
- Starting from head.next skips first node
- Setting current to None causes no traversal
- Using head.prev in singly linked list (no prev pointer)
class Node:
def __init__(self, data):
self.data = data
self.next = None
def search(head, target):
current = head
while current:
if current.data == target:
return True
current = current.next
return False
# Create linked list: 5 -> 10 -> 15 -> None
head = Node(5)
head.next = Node(10)
head.next.next = Node(15)
print(search(head, 10))
print(search(head, 20))Solution
Step 1: Trace search for 10
Nodes checked: 5 (no), 10 (yes) -> returns True.Step 2: Trace search for 20
Nodes checked: 5, 10, 15 (none match) -> returns False.Final Answer:
True\nFalse -> Option AQuick Check:
10 found, 20 not found [OK]
- Returning False too early
- Not moving to next node correctly
- Confusing True/False outputs
def search(head, target):
current = head
while current.next:
if current.data == target:
return True
current = current.next
return FalseSolution
Step 1: Analyze loop condition
Usingwhile current.nextskips checking the last node.Step 2: Correct loop to include last node
Change towhile currentto check all nodes including last.Final Answer:
The loop condition should be while current, not while current.next -> Option AQuick Check:
Loop must check all nodes [OK]
- Stopping before last node
- Returning False too early
- Starting current at head.next skips first node
Solution
Step 1: Traverse list once counting matches
Check each node; if data is 3, increase count.Step 2: After traversal, check if count > 0
If count is more than zero, value 3 exists and count shows occurrences.Final Answer:
Traverse the list once, increment count each time node data equals 3, then check if count > 0 -> Option DQuick Check:
Single pass count and check [OK]
- Doing two separate traversals
- Sorting linked list unnecessarily
- Stopping after first find without counting all
Ready to prove it?
Pass the test and this pattern moves unknown
