<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":499,"date":"2021-07-13T16:57:20","date_gmt":"2021-07-13T16:57:20","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=499"},"modified":"2021-09-13T06:50:32","modified_gmt":"2021-09-13T06:50:32","slug":"how-to-delete-an-element-in-a-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/","title":{"rendered":"How to delete an element in a linked list ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"how-to-delete-an-element-in-a-linked-list\" class=\"color-pink\" style=\"text-align: justify;\">How to delete an element in a linked list ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>To delete a node from\u00a0<a href=\"https:\/\/www.wikitechy.com\/tutorials\/java\/java-linked-list\" target=\"_blank\" rel=\"noopener\">linked list<\/a>, we need to do following steps:\n<ul>\n<li>Find previous node of the node to be deleted.<\/li>\n<li>Change the next of previous node.<\/li>\n<li>Free\u00a0memory\u00a0for the node to be deleted<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"ImageContent\" style=\"text-align: justify;\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png\" alt=\" \" \/><\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"example\" class=\"color-purple\">Example<\/h2>\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: position = 1, Linked List = 9-&gt;2-&gt;3-&gt;7-&gt;6<br\/>Output: Linked List =  9-&gt;3-&gt;7-&gt;6<br\/><br\/>Input: position = 0, Linked List = 9-&gt;2-&gt;3-&gt;7-&gt;6<br\/>Output: Linked List = 2-&gt;3-&gt;7-&gt;6<\/code><\/pre> <\/div>\n<h2 id=\"sample-code\" class=\"color-purple\">Sample code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">#include &lt;stdio.h&gt; <br\/>#include &lt;stdlib.h&gt; <br\/><br\/>\/\/ A linked list node <br\/>struct Node <br\/>{ <br\/>\tint data; <br\/>\tstruct Node *next; <br\/>}; <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\/>\tstruct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); <br\/>\tnew_node-&gt;data = new_data; <br\/>\tnew_node-&gt;next = (*head_ref); <br\/>\t(*head_ref) = new_node; <br\/>} <br\/><br\/>\/* Given a reference (pointer to pointer) to the head of a list <br\/>and a position, deletes the node at the given position *\/<br\/>void deleteNode(struct Node **head_ref, int position) <br\/>{ <br\/>\/\/ If linked list is empty <br\/>if (*head_ref == NULL) <br\/>\treturn; <br\/><br\/>\/\/ Store head node <br\/>struct Node* temp = *head_ref; <br\/><br\/>\t\/\/ If head needs to be removed <br\/>\tif (position == 0) <br\/>\t{ <br\/>\t\t*head_ref = temp-&gt;next; \/\/ Change head <br\/>\t\tfree(temp);\t\t\t \/\/ free old head <br\/>\t\treturn; <br\/>\t} <br\/><br\/>\t\/\/ Find previous node of the node to be deleted <br\/>\tfor (int i=0; temp!=NULL &amp;&amp; i&lt;position-1; i++) <br\/>\t\ttemp = temp-&gt;next; <br\/><br\/>\t\/\/ If position is more than number of ndoes <br\/>\tif (temp == NULL || temp-&gt;next == NULL) <br\/>\t\treturn; <br\/><br\/>\t\/\/ Node temp-&gt;next is the node to be deleted <br\/>\t\/\/ Store pointer to the next of node to be deleted <br\/>\tstruct Node *next = temp-&gt;next-&gt;next; <br\/><br\/>\t\/\/ Unlink the node from linked list <br\/>\tfree(temp-&gt;next); \/\/ Free memory <br\/><br\/>\ttemp-&gt;next = next; \/\/ Unlink the deleted node from list <br\/>} <br\/><br\/>\/\/ This function prints contents of linked list starting from <br\/>\/\/ the given node <br\/>void printList(struct Node *node) <br\/>{ <br\/>\twhile (node != NULL) <br\/>\t{ <br\/>\t\tprintf(&quot; %d &quot;, node-&gt;data); <br\/>\t\tnode = node-&gt;next; <br\/>\t} <br\/>} <br\/><br\/>\/* Drier program to test above functions*\/<br\/>int main() <br\/>{ <br\/>\t\/* Start with the empty list *\/<br\/>\tstruct Node* head = NULL; <br\/><br\/>\tpush(&amp;head, 6); <br\/>\tpush(&amp;head, 7); <br\/>\tpush(&amp;head, 3); <br\/>\tpush(&amp;head, 2); <br\/>\tpush(&amp;head, 9); <br\/><br\/>\tputs(&quot;Created Linked List: &quot;); <br\/>\tprintList(head); <br\/>\tdeleteNode(&amp;head, 4); <br\/>\tputs(&quot;\\nLinked List after Deletion at position 4: &quot;); <br\/>\tprintList(head); <br\/>\treturn 0; <br\/>}<\/code><\/pre> <\/div>\n<h2 id=\"output\" class=\"color-purple\">Output:<\/h2>\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\">Created Linked List: <br\/> 9  2  3  7  6 <br\/>Linked List after Deletion at position 4: <br\/> 9  2  3  7<\/code><\/pre> <\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : To delete a node from linked list&#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,15938,3493,3500,3498,3496,3495,3499,3503,3502,3432,205,222,484,3054,196,212,3501,207,366,204,3492,3497,206,972,200,3055,197,968,3056,285,969],"class_list":["post-499","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-compro-technologies-interview-questions-and-answers","tag-delete-a-node-from-linked-list-algorithm","tag-delete-a-node-in-linked-list-with-single-pointer","tag-delete-a-specific-node-in-linked-list-c","tag-delete-first-node-in-linked-list-c","tag-delete-node-at-given-position-in-a-linked-list","tag-delete-node-at-given-position-in-a-linked-list-c","tag-delete-node-at-given-position-in-a-linked-list-in-c","tag-delete-node-linked-list-c","tag-deletion-in-linked-list-in-data-structure","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","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-linked-list-delete-node-c","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-remove-element-from-linked-list-c","tag-remove-middle-element-linked-list-c","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-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>How to delete an element in a linked list ? - Data Structure<\/title>\n<meta name=\"description\" content=\"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.\" \/>\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\/how-to-delete-an-element-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=\"How to delete an element in a linked list ? - Data Structure\" \/>\n<meta property=\"og:description\" content=\"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T16:57:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-13T06:50:32+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png\" \/>\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\/how-to-delete-an-element-in-a-linked-list\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/\",\"name\":\"How to delete an element in a linked list ? - Data Structure\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png\",\"datePublished\":\"2021-07-13T16:57:20+00:00\",\"dateModified\":\"2021-09-13T06:50:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png\"},{\"@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":"How to delete an element in a linked list ? - Data Structure","description":"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.","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\/how-to-delete-an-element-in-a-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"How to delete an element in a linked list ? - Data Structure","og_description":"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T16:57:20+00:00","article_modified_time":"2021-09-13T06:50:32+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png"}],"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\/how-to-delete-an-element-in-a-linked-list\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/","name":"How to delete an element in a linked list ? - Data Structure","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png","datePublished":"2021-07-13T16:57:20+00:00","dateModified":"2021-09-13T06:50:32+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"How to delete an element in a linked list ? - Find previous node of the node to be deleted. Change the next of previous node.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/how-to-delete-an-element-in-a-linked-list\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/linkedlist-deletion.png"},{"@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\/499","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=499"}],"version-history":[{"count":4,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/499\/revisions"}],"predecessor-version":[{"id":3504,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/499\/revisions\/3504"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=499"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=499"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=499"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}