📖 Scenario: Imagine you have a list of tasks to do today. You want to remove a specific task once it's done. We will use a singly linked list to store tasks and learn how to delete a task by its name.
🎯 Goal: Build a singly linked list of tasks and write code to delete a task by its name (value). Finally, print the list to see the remaining tasks.
📋 What You'll Learn
Create a singly linked list with 4 tasks: 'Laundry', 'Groceries', 'Homework', 'Exercise'
Create a variable called taskToDelete with the value 'Homework'
Write a function deleteNodeByValue that deletes the first node with the value matching taskToDelete
Print the linked list after deletion in the format: Task1 -> Task2 -> ... -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many applications like task scheduling, undo functionality in editors, and managing playlists where dynamic insertion and deletion are common.
💼 Career
Understanding linked lists and how to manipulate them is fundamental for software developers, especially when working with low-level data structures or optimizing memory usage.
Progress0 / 4 steps
1
Create the Singly Linked List with Tasks
Create a class called Node with properties value and next. Then create a class called LinkedList with a property head initialized to null. Finally, create a linked list called tasks and add nodes with values 'Laundry', 'Groceries', 'Homework', and 'Exercise' in that order.
DSA Javascript
Hint
Define Node and LinkedList classes first. Use append method to add tasks.
2
Set the Task to Delete
Create a variable called taskToDelete and set it to the string 'Homework'.
DSA Javascript
Hint
Use const taskToDelete = 'Homework'; to create the variable.
3
Write the Delete Function
Add a method called deleteNodeByValue to the LinkedList class. This method should take a parameter value and delete the first node in the list whose value matches it. Handle the case when the node to delete is the head. Use variables current and previous to traverse the list.
DSA Javascript
Hint
Check if the head node has the value. If yes, move head to next. Otherwise, traverse with current and previous to find and remove the node.
4
Delete the Task and Print the List
Call the deleteNodeByValue method on tasks with taskToDelete as argument. Then print the linked list values separated by -> and ending with null. Use a variable current to traverse the list and build the output string.
DSA Javascript
Hint
Call tasks.deleteNodeByValue(taskToDelete); then traverse from tasks.head to build and print the string.
Practice
(1/5)
1. What is the main purpose of the deleteNodeByValue function in a linked list?
easy
A. To reverse the linked list
B. To add a new node with the given value at the end of the list
C. To find the position of a node with the given value
D. To remove the first node that contains the given value from the list
Solution
Step 1: Understand the function's goal
The function aims to remove a node that matches a specific value from the linked list.
Step 2: Identify the action performed
Deleting a node by value means finding the first node with that value and removing it from the list.
Final Answer:
To remove the first node that contains the given value from the list -> Option D
Quick Check:
Delete node by value = Remove node with value [OK]
Hint: Deleting by value means removing matching node, not adding [OK]
Common Mistakes:
Confusing deletion with insertion
Thinking it finds position only
Assuming it reverses the list
2. Which of the following is the correct way to check if the head node contains the value to delete in JavaScript?
easy
A. if (head.value === value) { /* delete head */ }
B. if (head == value) { /* delete head */ }
C. if (head.value = value) { /* delete head */ }
D. if (head.value != value) { /* delete head */ }
Solution
Step 1: Understand comparison syntax in JavaScript
Use triple equals (===) to compare values strictly without type coercion.
Step 2: Identify correct condition for head node value check
Check if head.value equals the target value using ===.
Final Answer:
if (head.value === value) { /* delete head */ } -> Option A
Quick Check:
Use === for value comparison [OK]
Hint: Use === to compare values, not = or == [OK]
Common Mistakes:
Using single = which assigns instead of compares
Using == which can cause unexpected type coercion
Comparing head node directly to value without .value
3. Consider the linked list: 10 -> 20 -> 30 -> 40 -> null. After calling deleteNodeByValue(head, 30), what will be the new list?
medium
A. 20 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 10 -> 20 -> 40 -> null
D. 10 -> 30 -> 40 -> null
Solution
Step 1: Locate the node with value 30
Traverse the list: 10, 20, then 30 is found as the third node.
Step 2: Remove the node with value 30
Adjust the previous node's next pointer (20's next) to point to 40, skipping 30.
Final Answer:
10 -> 20 -> 40 -> null -> Option C
Quick Check:
Deleted 30 node, list skips it [OK]
Hint: Skip node by linking previous to next [OK]
Common Mistakes:
Not updating previous node's next pointer
Deleting wrong node or none
Confusing node values or order
4. Given this code snippet to delete a node by value, what is the main bug?
function deleteNodeByValue(head, value) {
let current = head;
while (current.next !== null) {
if (current.next.value === value) {
current.next = current.next.next;
return head;
}
current = current.next;
}
return head;
}
medium
A. It causes an infinite loop when value is not found
B. It does not handle deleting the head node if it matches the value
C. It deletes all nodes with the given value instead of just one
D. It throws an error if the list is empty
Solution
Step 1: Analyze head node deletion case
The code starts checking from current.next, so it never checks if head itself has the value.
Step 2: Identify missing head deletion handling
If the head node contains the value, it won't be deleted, causing incorrect behavior.
Final Answer:
It does not handle deleting the head node if it matches the value -> Option B
Quick Check:
Head node deletion missing = Bug [OK]
Hint: Always check head node separately before loop [OK]
Common Mistakes:
Assuming loop covers head node
Ignoring empty list case
Thinking it deletes all matching nodes
5. You have a singly linked list: 5 -> 10 -> 15 -> 10 -> 20 -> null. You want to delete the first node with value 10. After deletion, what is the correct list?
hard
A. 5 -> 15 -> 10 -> 20 -> null
B. 5 -> 10 -> 15 -> 20 -> null
C. 5 -> 15 -> 20 -> null
D. 10 -> 15 -> 10 -> 20 -> null
Solution
Step 1: Identify the first node with value 10
Traverse from head: 5 (no), then 10 (yes) at second node.
Step 2: Delete the first 10 node
Link 5's next pointer to 15, skipping the first 10 node.
Final Answer:
5 -> 15 -> 10 -> 20 -> null -> Option A
Quick Check:
Only first 10 removed, rest remain [OK]
Hint: Delete first matching node only, keep others [OK]
Common Mistakes:
Deleting all nodes with value 10
Deleting wrong 10 node
Breaking list links incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.