Delete All Occurrences of a Given Key in a Linked List
Understand how to remove all nodes that match a given key in a linked list. This lesson guides you through using two pointers to efficiently traverse and modify the list, handling edge cases like deleting the head node, while maintaining linear time and constant space complexity.
We'll cover the following...
We'll cover the following...
Statement
We’re given the head of a linked list and a key. Delete all the nodes that contain the given key.
Note: The input linked list will not have cycles in it.
Example
The following example elaborates this problem further:
The linked list, after we delete key 72:
Sample input
[20, 14, 36, 11, 72, 41]
key = 72
Expected output
[20, 14, 36, 11, 41]