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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Delete all occurrences of a given key in a linked list
- Delete multiple occurrences of key in Linked list using double pointer
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Delete all the even nodes from a Doubly Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K
- Delete a Doubly Linked List node at a given position
- Delete a node in a Doubly Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Construct a Doubly linked linked list from 2D Matrix
- Sum of all nodes in a doubly linked list divisible by a given number K
- Product of all nodes in a doubly linked list divisible by a given number K
- Product of all prime nodes in a Doubly Linked List
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Doubly Linked List
- Move all occurrences of an element to end in a linked list