Delete Nodes From A Linked List
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
Now that we've implemented a linked list, can you write a method that will delete all nodes of a given value?

You're given the following standard structure as the definition of a linked list node:
class LinkedListNode {
constructor(val) {
this.val = val;
this.next = null;
}
}
The follow scenario is how we would invoke the method with a linked list 1 -> 2 -> 2 -> 5 -> 6. As you can see, running the method results in removing all 2s in the list.

// head = 1 -> 2 -> 2 -> 5 -> 6
// val = 2
removeNodes(head, val);
// 1 -> 5 -> 6
Constraints
- The value of the nodes as well as the value to be deleted are within integer limits (between
-1000000000and1000000000) - After deleting the nodes, you need to return the head of the new linked list
- The given linked list can be null
- In case all nodes are deleted, return null
- Expected time complexity :
O(n) - Expected Space complexity :
O(1)
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →