The Idea is to traverse the list while maintaining a prev pointer to track the previous node. If the current node contains the key, update the next pointer of the prev node to the current node, effectively removing it from the list. This process continues until the end of the list, ensuring all nodes with the specified key are removed.
Below is the implementation of the above idea:
Output
1 8
Time complexity: O(n), where n is the number of nodes in the list. Auxiliary Space: O(1)