Given a doubly linked list and a key x. The problem is to delete all occurrences of the given key x from the doubly linked list.
delAllOccurOfGivenKey(head_ref, x) if head_ref == NULL return Initialize current = head_ref Declare next while current != NULL if current->data == x next = current->next deleteNode(head_ref, current) current = next else current = current->next
The algorithm for deleteNode(head_ref, current) (which deletes the node using the pointer to the node) is discussed in this post.
Original Doubly linked list: 2 2 10 8 4 2 5 2 Doubly linked list after deletion of 2: 10 8 4 5
Time Complexity: O(n)
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.