Data Structures · Visualization
Linked List: Search
Linked List
Searching means walking the list from the head, checking each node's value one by one, until you either find a match or run out of nodes. Time: O(n). Space: O(1).
Big-O: Lookup O(n); insert/delete at head O(1).
Linked List: Search
Linked ListSearching means walking the list from the head, checking each node's value one by one, until you either find a match or run out of nodes. Unlike an array, you cannot jump to a middle node, so every search must move step by step through the links.
How Search Works
- Start a pointer, current, at the head.
- While current is not null, check if current's value matches the target.
- If it matches, return that node (found).
- Otherwise move current to current.next and repeat.
- If current becomes null, the value was not in the list.
Search Worked Example
Input: Search for 15 in head -> 5 -> 10 -> 15 -> 20 -> null
- current = 5, no match
- current = 10, no match
- current = 15, match found!
Output: Found at the 3rd node
Press ▶ Run to watch it step by step.
def traverse(head):
cur = head
while cur:
print(cur.val, end=" -> ")
cur = cur.next # follow next pointer
print("None")
# example: search 15 on [5, 10, 15, 20]Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Search Time & Space Complexity
When to Use Search
- Checking whether an item exists before inserting or deleting it
- Looking up a record by ID in a simple linked structure
- Validating membership in small or moderately sized lists
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.