0
0
DSA Pythonprogramming

Search for a Value in Linked List in DSA Python

Choose your learning style9 modes available
Mental Model
Look at each item one by one until you find the value or reach the end.
Analogy: Like flipping through pages of a book to find a specific word, checking each page in order.
head -> [1] -> [2] -> [3] -> null
           ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, search for value 2
Goal: Find if value 2 exists in the list and return True or False
Step 1: Check first node value
head -> [1] -> [2] -> [3] -> null
           ↑
Why: Start from the first node to see if it matches the search value
Step 2: Move to next node because 1 != 2
head -> [1] -> [2] -> [3] -> null
                ↑
Why: Current node value is not the target, so check the next node
Step 3: Found value 2 at current node
head -> [1] -> [2] -> [3] -> null
                ↑
Why: Current node value matches the search value, so stop searching
Result:
head -> [1] -> [2] -> [3] -> null
Found: True
Annotated Code
DSA Python
class Node:
    def __init__(self, val):
        self.val = val
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def append(self, val):
        new_node = Node(val)
        if not self.head:
            self.head = new_node
            return
        curr = self.head
        while curr.next:
            curr = curr.next
        curr.next = new_node

    def search(self, target):
        curr = self.head
        while curr:
            if curr.val == target:
                return True
            curr = curr.next
        return False

# Driver code
ll = LinkedList()
ll.append(1)
ll.append(2)
ll.append(3)
result = ll.search(2)
print(f"Found: {result}")
while curr:
Traverse nodes one by one until end
if curr.val == target:
Check if current node value matches target
curr = curr.next
Move to next node to continue search
OutputSuccess
Found: True
Complexity Analysis
Time: O(n) because in worst case we check each node once
Space: O(1) because we use only a few variables regardless of list size
vs Alternative: Compared to arrays, linked list search is similar in time but does not allow direct access by index
Edge Cases
Empty list
Returns False immediately because there are no nodes
DSA Python
while curr:
Value not in list
Traverses entire list and returns False
DSA Python
return False
Value at head node
Returns True immediately without further traversal
DSA Python
if curr.val == target:
When to Use This Pattern
When you need to find if a value exists in a linked list, use linear search by checking each node one by one.
Common Mistakes
Mistake: Stopping search too early without checking all nodes
Fix: Continue traversing until you find the value or reach the end
Mistake: Not handling empty list causing errors
Fix: Check if head is None before starting traversal
Summary
Searches for a value by checking each node in order.
Use when you want to find if a value exists in a linked list.
The key is to move node by node until you find the value or reach the end.