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
↑head -> [1] -> [2] -> [3] -> null
↑head -> [1] -> [2] -> [3] -> null
↑head -> [1] -> [2] -> [3] -> null
↑head -> [1] -> [2] -> [3] -> null
↑head -> [1] -> [2] -> [3] -> null Found: True
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:if curr.val == target:curr = curr.nextwhile curr:return False
if curr.val == target: