A linked list is a chain of nodes where each node holds data and a link to the next node. It is like a treasure hunt where each clue leads to the next one.
Click to reveal answer
beginner
How do you search for a value in a linked list?
Start at the first node and check its value. If it matches, stop. If not, move to the next node. Repeat until you find the value or reach the end.
Click to reveal answer
beginner
What happens if the value is not found in the linked list?
You reach the end of the list (null) without finding the value. This means the value is not in the list.
Click to reveal answer
intermediate
Why is searching in a linked list slower than in an array?
Because you must check nodes one by one from the start. Unlike arrays, linked lists do not allow jumping directly to the middle.
Click to reveal answer
beginner
Show the JavaScript code snippet to search a value in a singly linked list.
function searchLinkedList(head, target) {
let current = head;
while (current !== null) {
if (current.value === target) {
return true;
}
current = current.next;
}
return false;
}
Click to reveal answer
What does each node in a linked list contain?
AData and a link to the next node
BOnly data
COnly a link to the next node
DData and a link to the previous node only
✗ Incorrect
Each node holds data and a link to the next node, forming the chain.
What is the first step to search for a value in a linked list?
AJump to the middle node
BStart from the last node
CStart from the first node
DSort the list first
✗ Incorrect
Searching starts from the first node and moves forward.
If the value is not found in the linked list, what will the search function return?
ATrue
BFalse
CNull
DThe last node
✗ Incorrect
The function returns false if the value is not found.
Why can't you jump directly to the middle of a linked list like in an array?
ABecause nodes only link to the next node, no direct access
BBecause linked lists are unordered
CBecause linked lists are sorted
DBecause arrays are slower
✗ Incorrect
Linked lists have no direct access; you must follow links node by node.
What is the time complexity of searching a value in a linked list?
AO(1)
BO(log n)
CO(n^2)
DO(n)
✗ Incorrect
Searching requires checking nodes one by one, so it is O(n).
Explain how to search for a value in a singly linked list step-by-step.
Think of walking through a chain of people asking for a name.
You got /4 concepts.
Write a simple JavaScript function to check if a value exists in a linked list.
Use a while loop and check current node's value.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of searching for a value in a linked list?
easy
A. To find if the value exists in the list
B. To add a new node at the end
C. To delete the first node
D. To reverse the linked list
Solution
Step 1: Understand the search operation
Searching in a linked list means checking each node to see if it contains the target value.
Step 2: Identify the goal of searching
The goal is to find whether the value exists anywhere in the list, not to modify the list.
Final Answer:
To find if the value exists in the list -> Option A
Quick Check:
Search means find value presence = C [OK]
Hint: Search means check nodes for value presence [OK]
Common Mistakes:
Confusing search with insertion or deletion
Thinking search changes the list
Assuming search reverses the list
2. Which of the following is the correct way to start searching a linked list in JavaScript if head is the first node?
easy
A. let current = null;
B. let current = head;
C. let current = head.next.next;
D. let current = head.value;
Solution
Step 1: Understand the starting point of search
Search starts from the first node, so we assign the first node to a variable to traverse.
Step 2: Choose the correct assignment
Assigning head to current lets us start from the first node and move forward.
Final Answer:
let current = head; -> Option B
Quick Check:
Start search at head node = B [OK]
Hint: Start search at head node with let current = head [OK]
Common Mistakes:
Starting at null means no nodes to check
Skipping nodes by starting at head.next.next
Assigning value instead of node reference
3. What will be the output of the following code snippet?
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
const head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
function search(head, target) {
let current = head;
while (current !== null) {
if (current.value === target) {
return true;
}
current = current.next;
}
return false;
}
console.log(search(head, 10));
console.log(search(head, 20));
medium
A. true\ntrue
B. false\ntrue
C. true\nfalse
D. false\nfalse
Solution
Step 1: Trace search for value 10
Nodes are 5 -> 10 -> 15. The search finds 10 in the second node and returns true.
Step 2: Trace search for value 20
20 is not in the list, so the loop ends and returns false.
Final Answer:
true\nfalse -> Option C
Quick Check:
10 found = true, 20 not found = false [OK]
Hint: Return true if found, else false after full traversal [OK]
Common Mistakes:
Returning false too early
Not updating current to next node
Confusing value with node reference
4. Identify the error in the following search function for a linked list:
function search(head, target) {
let current = head;
while (current.next !== null) {
if (current.value === target) {
return true;
}
current = current.next;
}
return false;
}
medium
A. It does not check the last node's value
B. It uses incorrect variable names
C. It returns true too early
D. It never updates current
Solution
Step 1: Analyze the loop condition
The loop runs while current.next !== null, so it stops before the last node.
Step 2: Check if last node is checked
The last node's value is never checked because loop ends early, so search may miss target at last node.
Final Answer:
It does not check the last node's value -> Option A
Quick Check:
Loop misses last node check = D [OK]
Hint: Loop until current is null, not current.next [OK]
Common Mistakes:
Using current.next !== null instead of current !== null
Forgetting to check last node
Assuming loop covers all nodes
5. Given a linked list where each node contains a number, how would you modify the search function to return the position (1-based) of the first occurrence of a target value, or -1 if not found?
hard
A. Reverse the list before searching for the value
B. Return true if value found; else false
C. Count total nodes first; then search for value
D. Initialize a position counter at 1; traverse nodes; return position if value matches; else -1
Solution
Step 1: Add a position counter starting at 1
Initialize a variable to track the current node's position as you traverse the list.
Step 2: Traverse and compare values
For each node, check if its value matches the target. If yes, return the current position.
Step 3: Return -1 if not found
If traversal ends without finding the target, return -1 to indicate absence.
Final Answer:
Initialize a position counter at 1; traverse nodes; return position if value matches; else -1 -> Option D
Quick Check:
Position tracking with traversal = A [OK]
Hint: Track position while searching; return position or -1 [OK]
Common Mistakes:
Returning boolean instead of position
Not updating position counter
Reversing list unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.