{"id":681,"date":"2025-07-18T14:12:47","date_gmt":"2025-07-18T08:42:47","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=681"},"modified":"2025-07-18T14:12:49","modified_gmt":"2025-07-18T08:42:49","slug":"delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/","title":{"rendered":"Delete all occurrences of a given key in a doubly linked list | GFG Practice"},"content":{"rendered":"\n<p>Given a doubly linked list and a key, delete all occurrences of the given\u00a0key from the doubly linked list.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> <br>2&lt;->2&lt;->10&lt;->8&lt;->4&lt;->2&lt;->5&lt;->2<br>2<br><strong>Output:<\/strong> <br>10&lt;->8&lt;->4&lt;->5<br><strong>Explanation: <\/strong><br>All Occurences of 2 have been deleted.<\/pre>\n\n\n\n<p><strong>Example2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> \n9&lt;-&gt;1&lt;-&gt;3&lt;-&gt;4&lt;-&gt;5&lt;-&gt;1&lt;-&gt;8&lt;-&gt;4\n9\n<strong>Output:<\/strong> \n1&lt;-&gt;3&lt;-&gt;4&lt;-&gt;5&lt;-&gt;1&lt;-&gt;8&lt;-&gt;4\n<strong>Explanation: <\/strong>\nAll Occurences of 9 have been deleted.<\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><\/p>\n\n\n\n<p>Complete the function void&nbsp;<strong>deleteAllOccurOfX(struct Node** head_ref, int key)<\/strong>, which takes the reference of the head pointer and an integer value key. Delete all occurrences of the key from the given DLL.<\/p>\n\n\n\n<p><strong>Expected Time Complexity:<\/strong>\u00a0O(N).<br><strong>Expected Auxiliary Space:<\/strong>\u00a0O(1).<\/p>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1&lt;=Number of Nodes&lt;=10<sup>5<\/sup><\/li>\n\n\n\n<li>0&lt;=Node Value &lt;=10<sup>9<\/sup><\/li>\n<\/ul>\n\n\n<div class=\"wp-block-ub-table-of-contents-block ub_table-of-contents\" id=\"ub_table-of-contents-7af31ddc-5f37-4f80-b0b3-3cd5dd663c26\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"false\" data-initiallyhideonmobile=\"false\" data-initiallyshow=\"true\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column \">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#0-solution-traversal-and-deletion-approach\" style=\"\">Solution: Traversal and Deletion Approach<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#8-important-note\" style=\"\">Important Note<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list\/#9-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-traversal-and-deletion-approach\">Solution: Traversal and Deletion Approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like removing all instances of a specific word from a sentence written on sticky notes connected to each other! Since it&#8217;s a doubly linked list, each note (node) is connected to both its previous and next notes. When we find a note with the word we want to remove, we need to carefully disconnect it and reconnect its neighbors to each other. We also need to be careful about updating the head if we&#8217;re removing the first note.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Handle Single Node Case<\/strong>: If there&#8217;s only one node and it matches the key, return null<\/li>\n\n\n\n<li><strong>Initialize Pointers<\/strong>: Set up temp pointer to traverse and previous pointer to track the previous node<\/li>\n\n\n\n<li><strong>Traverse the List<\/strong>: Go through each node and check if its data matches the key<\/li>\n\n\n\n<li><strong>Delete Matching Nodes<\/strong>: When found, update the links of previous and next nodes to skip current node<\/li>\n\n\n\n<li><strong>Update Head<\/strong>: If we&#8217;re deleting the head node, update the new head<\/li>\n\n\n\n<li><strong>Continue Traversal<\/strong>: Keep moving forward until we reach the end<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    #Function to delete all the occurances of a key from the linked list.\n    def deleteAllOccurOfX(self, head, k):\n        # Handle special case: single node that matches the key\n        if not head.next and head.data == k:\n            return None\n        \n        temp = head          # Pointer to traverse the list\n        previous = None      # Pointer to track previous node\n        new_head = head      # Keep track of new head\n        \n        # Traverse through the entire list\n        while temp is not None:\n            if temp.data == k:  # Found a node to delete\n                # Update previous node's next pointer\n                if previous:\n                    previous.next = temp.next\n                \n                # Update next node's prev pointer\n                if temp.next:\n                    temp.next.prev = previous\n                \n                # Update head if we're deleting the first node\n                if temp == new_head:\n                    new_head = new_head.next\n            \n            previous = temp      # Move previous pointer\n            temp = temp.next     # Move to next node\n        \n        return new_head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">#Function to delete all the occurances of a key from the linked list.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">deleteAllOccurOfX<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">k<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Handle special case: single node that matches the key<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> head.next <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> head.data == k:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        temp = head          <\/span><span style=\"color: #6A9955\"># Pointer to traverse the list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        previous = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">      <\/span><span style=\"color: #6A9955\"># Pointer to track previous node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        new_head = head      <\/span><span style=\"color: #6A9955\"># Keep track of new head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Traverse through the entire list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> temp <\/span><span style=\"color: #569CD6\">is<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">not<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.data == k:  <\/span><span style=\"color: #6A9955\"># Found a node to delete<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Update previous node&#39;s next pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> previous:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    previous.next = temp.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Update next node&#39;s prev pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    temp.next.prev = previous<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Update head if we&#39;re deleting the first node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> temp == new_head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    new_head = new_head.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            previous = temp      <\/span><span style=\"color: #6A9955\"># Move previous pointer<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            temp = temp.next     <\/span><span style=\"color: #6A9955\"># Move to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> new_head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>This solution traverses the doubly linked list and deletes all nodes that match the given key. The algorithm handles the special case where there&#8217;s only one node that matches the key. During traversal, when a matching node is found, it updates the links of the previous and next nodes to skip the current node. The tricky part is properly updating the head pointer when the first node needs to be deleted. However, there&#8217;s a subtle issue in this implementation: the previous pointer is updated even when we delete a node, which can lead to incorrect link management in some cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 &lt;-&gt; 2 &lt;-&gt; 3 &lt;-&gt; 2 &lt;-&gt; 4<\/code>, delete key = 2<\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp = 1, previous = None, new_head = 1<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp.data = 1, not equal to key (2)<\/li>\n\n\n\n<li>previous = 1, temp = 2<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp.data = 2, equal to key<\/li>\n\n\n\n<li>previous.next = temp.next (node 3)<\/li>\n\n\n\n<li>temp.next.prev = previous (node 3&#8217;s prev becomes node 1)<\/li>\n\n\n\n<li>temp != new_head, so no head change<\/li>\n\n\n\n<li>Links now: 1 &lt;-> 3 &lt;-> 2 &lt;-> 4 (first 2 is bypassed)<\/li>\n\n\n\n<li>previous = 2 (deleted node), temp = 3<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp.data = 3, not equal to key<\/li>\n\n\n\n<li>previous = 3, temp = 2 (second occurrence)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp.data = 2, equal to key<\/li>\n\n\n\n<li>previous.next = temp.next (node 4)<\/li>\n\n\n\n<li>temp.next.prev = previous (node 4&#8217;s prev becomes node 3)<\/li>\n\n\n\n<li>temp != new_head, so no head change<\/li>\n\n\n\n<li>Links now: 1 &lt;-> 3 &lt;-> 4 (second 2 is bypassed)<\/li>\n\n\n\n<li>previous = 2 (deleted node), temp = 4<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 5:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>temp.data = 4, not equal to key<\/li>\n\n\n\n<li>previous = 4, temp = None<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 &lt;-&gt; 3 &lt;-&gt; 4<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n) &#8211; We traverse the entire list once<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1) &#8211; We only use a few pointer variables<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like going through a chain of connected items and removing all items of a specific type. Since each item is connected to both its neighbors, when we remove an item, we need to make sure its neighbors are properly connected to each other. We also need to remember to update our starting point if we remove the first item in the chain.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-important-note\">Important Note<\/h2>\n\n\n\n<p>While this solution works for most cases, there&#8217;s a subtle issue with the pointer management. The&nbsp;<code>previous<\/code>&nbsp;pointer is updated even when we delete a node, which can potentially cause issues in edge cases. A more robust approach would be to only update the&nbsp;<code>previous<\/code>&nbsp;pointer when we don&#8217;t delete the current node.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Traversal and Deletion<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Understanding doubly linked list operations<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This solution demonstrates the key concepts of doubly linked list manipulation, managing both forward and backward links when deleting nodes. The main challenge is properly handling the pointer updates and edge cases like deleting the head node or all nodes in the list.<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given a doubly linked list and a key, delete all occurrences of the given\u00a0key from the doubly linked list. Here&#8217;s the [Problem Link] to begin with. Example1: Input: 2&lt;->2&lt;->10&lt;->8&lt;->4&lt;->2&lt;->5&lt;->22Output: 10&lt;->8&lt;->4&lt;->5Explanation: All Occurences of 2 have been deleted. Example2: Input: 9&lt;-&gt;1&lt;-&gt;3&lt;-&gt;4&lt;-&gt;5&lt;-&gt;1&lt;-&gt;8&lt;-&gt;4 9 Output: 1&lt;-&gt;3&lt;-&gt;4&lt;-&gt;5&lt;-&gt;1&lt;-&gt;8&lt;-&gt;4 Explanation: All Occurences of 9 have been deleted. Your Task: Complete the<\/p>\n","protected":false},"author":1,"featured_media":682,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[30,19],"class_list":{"0":"post-681","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-doubly-linked-list","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/681","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=681"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/681\/revisions"}],"predecessor-version":[{"id":683,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/681\/revisions\/683"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/682"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=681"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=681"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=681"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}