Step 5 of 6

83% Complete

Searching Operations

Learn how to search for elements in a linked list

Searching in a Linked List

Searching is a fundamental operation in linked lists. There are two common search operations:

  1. Search by value: Find a node with a specific value
  2. Search by position: Find a node at a specific position

Let's explore both of these operations.

1. Search by Value

Searching for a node with a specific value requires traversing the list:

  1. Start at the head of the list
  2. Traverse the list, comparing each node's value with the target value
  3. Return the position if found, or -1 if not found
Search by Value
search(value) {
// Start at the head of the list
let current = this.head;
let position = 0;
// Traverse the list
while (current) {
// If the current node's value matches the target, return the position
if (current.value === value) {
return position;
}
// Move to the next node
current = current.next;
position++;
}
// If the value is not found, return -1
return -1;
}
Searching for value 30 in the list:

The linked list is empty

Time Complexity: O(n) - In the worst case, we need to traverse the entire list to find the target value or determine it's not present.

2. Search by Position

Searching for a node at a specific position also requires traversing the list:

  1. Start at the head of the list
  2. Traverse the list until reaching the desired position
  3. Return the node's value if found, or null if the position is invalid
Search by Position
getAt(position) {
// Start at the head of the list
let current = this.head;
let count = 0;
// Traverse the list until reaching the desired position
while (current) {
if (count === position) {
return current.value;
}
// Move to the next node
current = current.next;
count++;
}
// If the position is invalid, return null
return null;
}
Getting the node at position 3 in the list:

The linked list is empty

Time Complexity: O(n) - In the worst case, we need to traverse to the end of the list, making this a linear-time operation.

Optimizing Search Operations

Linked lists are not optimized for search operations. Here are some considerations:

  • No random access: Unlike arrays, linked lists don't support direct indexing, so we always need to traverse from the head.
  • No binary search: Since we can't jump to the middle of a linked list in constant time, we can't use binary search algorithms.
  • Alternative data structures: If your application requires frequent searches, consider using arrays, hash tables, or binary search trees instead.

Performance Comparison

Operation
Linked List
Array
Search by Value
O(n)
O(n)
Search by Position
O(n)
O(1)
Binary Search
Not applicable
O(log n)

Interactive Example

Try searching for values in the linked list below:

The linked list is empty

Use the controls to add nodes

Check Your Understanding

Next Steps

Now that you understand how to search for elements in a linked list, let's move on to learning about traversal operations in the next tutorial.