<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":509,"date":"2021-07-13T17:39:53","date_gmt":"2021-07-13T17:39:53","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=509"},"modified":"2021-09-13T06:23:08","modified_gmt":"2021-09-13T06:23:08","slug":"delete-all-occurrences-of-a-given-key-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/","title":{"rendered":"Delete all occurrences of a given key in a linked list ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"delete-all-occurrences-of-a-given-key-in-a-linked-list\" class=\"color-pink\">Delete all occurrences of a given key in a linked list ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"CodeContent\">\n<div class=\"hddn\">\n<ul>\n<li>Given a\u00a0singly linked list, delete all occurrences of a given key in it. For example,<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-markdown code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markdown code-embed-code\">Input:  2 -&gt; 1 -&gt; 8 -&gt; 2 -&gt;  3 -&gt;  2 -&gt; 7<br\/>       Key to delete = 2<br\/>Output:  1 -&gt; 8 -&gt; 3 -&gt; 7 <\/code><\/pre> <\/div>\n<h2 id=\"sample-code-in-c\" class=\"color-purple\">Sample Code in C<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt; <br\/>#include &lt;stdlib.h&gt; <br\/>\/\/ A linked list node <br\/>struct Node <br\/>{ <br\/>    int data; <br\/>    struct Node *next; <br\/>}; <br\/>\/* Given a reference (pointer to pointer) to the head of a list <br\/>   and an int, inserts a new node on the front of the list. *\/<br\/>void push(struct Node** head_ref, int new_data) <br\/>{ <br\/>    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); <br\/>    new_node-&gt;data  = new_data; <br\/>    new_node-&gt;next = (*head_ref); <br\/>    (*head_ref)    = new_node; <br\/>} <br\/>\/* Given a reference (pointer to pointer) to the head of a list and <br\/>   a key, deletes all occurrence of the given key in linked list *\/<br\/>void deleteKey(struct Node **head_ref, int key) <br\/>{ <br\/>    \/\/ Store head node <br\/>    struct Node* temp = *head_ref, *prev; <br\/>    \/\/ If head node itself holds the key or multiple occurrences of key <br\/>    while (temp != NULL &amp;&amp; temp-&gt;data == key) <br\/>    { <br\/>        *head_ref = temp-&gt;next;   \/\/ Changed head <br\/>        free(temp);               \/\/ free old head <br\/>        temp = *head_ref;         \/\/ Change Temp <br\/>    } <br\/>    \/\/ Delete occurrences other than head <br\/>    while (temp != NULL) <br\/>    { <br\/>        \/\/ Search for the key to be deleted, keep track of the <br\/>        \/\/ previous node as we need to change &#039;prev-&gt;next&#039; <br\/>        while (temp != NULL &amp;&amp; temp-&gt;data != key) <br\/>        { <br\/>            prev = temp; <br\/>            temp = temp-&gt;next; <br\/>        } <br\/>        \/\/ If key was not present in linked list <br\/>        if (temp == NULL) return; <br\/>        \/\/ Unlink the node from linked list <br\/>        prev-&gt;next = temp-&gt;next; <br\/>        free(temp);  \/\/ Free memory <br\/>        \/\/Update Temp for next iteration of outer loop <br\/>        temp = prev-&gt;next; <br\/>    } <br\/>} <br\/>\/\/ This function prints contents of linked list starting from <br\/>\/\/ the given node <br\/>void printList(struct Node *node) <br\/>{ <br\/>    while (node != NULL) <br\/>    { <br\/>        printf(&quot; %d &quot;, node-&gt;data); <br\/>        node = node-&gt;next; <br\/>    } <br\/>} <br\/>   \/* Drier program to test above functions*\/<br\/>int main() <br\/>{ <br\/>    \/* Start with the empty list *\/<br\/>    struct Node* head = NULL; <br\/>    push(&amp;head, 7); <br\/>    push(&amp;head, 2); <br\/>    push(&amp;head, 3); <br\/>    push(&amp;head, 2); <br\/>    push(&amp;head, 8); <br\/>    push(&amp;head, 1); <br\/>    push(&amp;head, 2); <br\/>    push(&amp;head, 2); <br\/>    int key = 2; \/\/ key to delete <br\/>    puts(&quot;Created Linked List: &quot;); <br\/>    printList(head); <br\/>    deleteKey(&amp;head, key); <br\/>    puts(&quot;\\nLinked List after Deletion: &quot;); <br\/>    printList(head); <br\/>    return 0; <br\/>}<\/code><\/pre> <\/div>\n<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-purple\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"Output\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code class=\"hljs\" data-lang=\"\"><span class=\"nt\">Created Linked List:\r\n 2  2  1  8  2  3  2  7\r\nLinked List after Deletion:\r\n 1  8  3  7 <\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Given a singly linked list, delete all occurrences&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[3028],"tags":[195,971,491,221,368,3494,203,199,214,198,363,3057,3601,3290,3292,3496,3289,3603,3602,3503,3286,205,3604,222,484,3054,196,212,207,366,204,3605,15925,206,972,200,3055,197,968,3056,3600,3606,3599,285,969],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-accenture-interview-questions-and-answers","tag-altimetrik-india-pvt-ltd-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-bharti-airtel-interview-questions-and-answers","tag-bmc-software-interview-questions-and-answers","tag-c-program-to-delete-first-node-in-linked-list","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-te-interview-questions-and-answers","tag-delete-a-specific-node-in-linked-list-java","tag-delete-all-nodes-in-doubly-linked-list-c","tag-delete-at-position-in-a-doubly-linked-list","tag-delete-first-node-in-linked-list-c","tag-delete-last-node-in-doubly-linked-list-in-c","tag-delete-last-node-in-linked-list-c","tag-delete-last-node-in-linked-list-in-c","tag-delete-node-at-given-position-in-a-linked-list-in-c","tag-delete-node-from-doubly-linked-list-java","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-doubly-linked-list-geeksforgeeks","tag-flipkart-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-globallogic-india-pvt-ltd-interview-questions-and-answers","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-remove-element-from-doubly-linked-list-c","tag-samsung-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-sapient-consulting-pvt-ltd-interview-questions-and-answers","tag-tech-mahindra-interview-questions-and-answers","tag-tracxn-technologies-pvt-ltd-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-wipro-infotech-interview-questions-and-answers","tag-wm-global-technology-services-india-pvt-ltd-limited-wmgts-interview-questions-and-answers","tag-write-a-program-to-delete-a-node-from-linked-list-in-c","tag-write-ac-program-to-delete-a-particular-element-in-the-doubly-linked-list","tag-write-an-algorithm-to-delete-duplicate-elements-in-a-singly-linked-list","tag-xoriant-solutions-pvt-ltd-interview-questions-and-answers","tag-yodlee-infotech-pvt-ltd-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Delete all occurrences of a given key in a linked list ? - Data Structure<\/title>\n<meta name=\"description\" content=\"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Delete all occurrences of a given key in a linked list ? - Data Structure\" \/>\n<meta property=\"og:description\" content=\"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T17:39:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-13T06:23:08+00:00\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/\",\"name\":\"Delete all occurrences of a given key in a linked list ? - Data Structure\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"datePublished\":\"2021-07-13T17:39:53+00:00\",\"dateModified\":\"2021-09-13T06:23:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Delete all occurrences of a given key in a linked list ? - Data Structure","description":"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Delete all occurrences of a given key in a linked list ? - Data Structure","og_description":"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T17:39:53+00:00","article_modified_time":"2021-09-13T06:23:08+00:00","author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/","name":"Delete all occurrences of a given key in a linked list ? - Data Structure","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"datePublished":"2021-07-13T17:39:53+00:00","dateModified":"2021-09-13T06:23:08+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"Delete all occurrences of a given key in a linked list ? - Data Structure - Given a singly linked list, delete all occurrences of a given key in it.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/delete-all-occurrences-of-a-given-key-in-a-linked-list\/"]}]},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=509"}],"version-history":[{"count":2,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":3491,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/509\/revisions\/3491"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}