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)
This article is contributed by Ayush Jauhari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Delete all occurrences of a given key in a linked list
- Delete all the even nodes from a Doubly Linked List
- Delete a node in a Doubly Linked List
- Delete multiple occurrences of key in Linked list using double pointer
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete a Doubly Linked List node at a given position
- Delete all the nodes from the doubly linked list that are greater than a given value
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Move all occurrences of an element to end in a linked list
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List | Set-2