{"id":4949,"date":"2021-09-13T02:48:17","date_gmt":"2021-09-13T02:48:17","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4949"},"modified":"2023-11-24T09:34:40","modified_gmt":"2023-11-24T09:34:40","slug":"delete-the-last-occurrence-of-an-item-from-the-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/","title":{"rendered":"Delete the last occurrence of an item from the linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\" alt=\"\" \/><\/p>\n<p>Linked lists are fundamental data structures in computer science, known for their flexibility and dynamic memory allocation. Deleting the last occurrence of a specific item within a linked list involves traversing the list to find the last instance of that item and then removing it while maintaining the list&#8217;s structural integrity. This operation requires careful consideration of the pointers that connect each element of the linked list. In this article, we&#8217;ll explore methods to efficiently delete the final occurrence of an item in a linked list using various algorithms and techniques. Basically, we are given a linked list and asked to delete the last occurrence of an item X from the linked list.<\/p>\n<h2>How to Delete the Last Occurrence of an Item from Linked List?<\/h2>\n<p>We have been given a linked list and a value X. We have to delete the last occurrence of the item X from the linked list. <\/p>\n<p>Let\u2019s understand this problem with an examples.<\/p>\n<p>If the linked list given to us is: head\u21921\u21922\u21927\u21925\u21922\u219210 and X = 2.<\/p>\n<ul>\n<li>According to the problem statement, first, we will have to find the last occurrence of <strong>X = 2<\/strong> in the linked list, i.e., last node towards the end of the linked list which had <strong>node -&gt; data == X<\/strong>, and then we will have to delete it from the linked list.<\/li>\n<li>Our linked list after deleting the last occurrence of <strong>X = 2<\/strong> will be: head\u21921\u21922\u21927\u21925\u219210.<\/li>\n<\/ul>\n<p>Suppose if the linked list is: head\u21921\u21923\u21925\u21927\u21927\u21927 and X=7.<\/p>\n<ul>\n<li>Then, in this case, after deleting the last occurrence of X = 7 from the linked list our linked list will be head\u21921\u21923\u21925\u21927\u21927.<\/li>\n<\/ul>\n<h5>Some more examples:<\/h5>\n<p>Sample Input 1: head\u21921\u21921\u21923\u21925\u21921\u21929, X = 1.<br \/>\nSample Output 1: head\u21921\u21921\u21923\u21925\u21929<\/p>\n<p>Sample Input 2: head\u21921\u21923\u21923\u21925\u21923\u21929\u21923\u21925\u21923\u21923, X = 3.<br \/>\nSample Output 2: head\u21921\u21923\u21923\u21925\u21923\u21929\u21923\u21925\u21923<\/p>\n<p>Now I think from the above example, the problem statement is clear. So let&#8217;s see how we will approach it.<\/p>\n<p>Before moving to the approach section, try to think about how you can approach this problem. <\/p>\n<ul>\n<li>If stuck, no problem, we will thoroughly see how we can approach the problem in the next section.<\/li>\n<\/ul>\n<p>Let\u2019s move to the approach section.<\/p>\n<h3>Approach to delete the last occurrence of an item from the linked list<\/h3>\n<p>Our approach will be simple:<\/p>\n<ul>\n<li>The idea is to initialize a special pointer variable and start traversing through the linked list. <\/li>\n<li>During the traversal, Whenever the current node\u2019s data is matching with value X, i.e., <strong>node -&gt; data == X<\/strong>, move the special pointer to the current node.<\/li>\n<li>When we reach the end of the linked list, the special pointer will be pointing to the last occurrence of X in the linked list.<\/li>\n<li>Now we will have to delete the node to which special pointer is pointing.<\/li>\n<li>Finally, after deleting the node to which the special pointer points, our objective will be satisfied.<\/li>\n<\/ul>\n<p>Let&#8217;s move to the algorithm section.<\/p>\n<h3>Algorithm to delete the last occurrence of an item from the linked list<\/h3>\n<ul>\n<li>Initialize a pointer variable named <strong>special<\/strong> with NULL.<\/li>\n<li>Start traversing through the linked list.<\/li>\n<li>If the current node data is equal to value X <strong>(curr-&gt;data == X)<\/strong>, move the special pointer to this current node.<\/li>\n<li>Continue the above step till the end of the linked list.<\/li>\n<li>When we reach the end, At that point of time, the special pointer points to the node that is the last occurring node with value X in the linked list.<\/li>\n<li>Now we have to delete that node.<\/li>\n<li>For deletion of that node, run a loop and reach the node prior to the node containing the last occurrence, i.e., the node to which special pointer is pointing. <\/li>\n<li>Now link the node before the special pointer and node after the special pointer together, and delete the special node.<\/li>\n<li>Finally, after deleting the node, our objective of deleting the last occurrence of X from the linked list will be satisfied.<\/li>\n<\/ul>\n<h3>Dry Run on delete the last occurrence of an item from the linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list-1.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/Delete-all-occurrences-of-a-given-key-in-a-doubly-linked-list-3.png\" alt=\"\" \/><\/p>\n<h3>Implementation of delete the last occurrence of an item from the linked list <\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4899 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4899 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4899 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4899 .wpsm_nav-tabs > li.active > a, #tab_container_4899 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4899 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4899 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4899 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4899 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4899 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4899 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4899 .wpsm_nav-tabs > li > a:hover , #tab_container_4899 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4899 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4899 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4899 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4899 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4899 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4899 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4899 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4899 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4899 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4899 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4899 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4899\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4899\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4899_1\" aria-controls=\"tabs_desc_4899_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4899_2\" aria-controls=\"tabs_desc_4899_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4899_3\" aria-controls=\"tabs_desc_4899_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4899_4\" aria-controls=\"tabs_desc_4899_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4899\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4899_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n \r\n\/\/ A linked list Node\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\nvoid deleteLast(struct Node* head, int x)\r\n{\r\n    struct Node *temp = head, *ptr = NULL;\r\n    while (temp) {\r\n \r\n        if (temp-&gt;data == x)\r\n            ptr = temp;       \r\n        temp = temp-&gt;next;\r\n    }\r\n \r\n    \/\/ If the last occurrence is the last node\r\n    if (ptr != NULL &amp;&amp; ptr-&gt;next == NULL) {\r\n        temp = head;\r\n        while (temp-&gt;next != ptr)\r\n            temp = temp-&gt;next;      \r\n        temp-&gt;next = NULL;\r\n    }\r\n \r\n    \/\/ If it is not the last node\r\n    if (ptr != NULL &amp;&amp; ptr-&gt;next != NULL) {\r\n        ptr-&gt;data = ptr-&gt;next-&gt;data;\r\n        temp = ptr-&gt;next;\r\n        ptr-&gt;next = ptr-&gt;next-&gt;next;\r\n        free(temp);\r\n    }\r\n}\r\n \r\nstruct Node* newNode(int x)\r\n{\r\n    struct Node* node = malloc(sizeof(struct Node*));\r\n    node-&gt;data = x;\r\n    node-&gt;next = NULL;\r\n    return node;\r\n}\r\n \r\nvoid display(struct Node* head)\r\n{\r\n    struct Node* temp = head;\r\n    if (head == NULL) {\r\n        printf(&quot;NULL&#92;n&quot;);\r\n        return;\r\n    }\r\n    while (temp != NULL) {\r\n        printf(&quot;%d -&gt; &quot;, temp-&gt;data);\r\n        temp = temp-&gt;next;\r\n    }\r\n    printf(&quot;NULL&#92;n&quot;);\r\n}\r\n\r\nint main()\r\n{\r\n    struct Node* head = newNode(1);\r\n    head-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next = newNode(7);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(5);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(10);\r\n    printf(&quot;Input Linked list: &quot;);\r\n    display(head);\r\n    deleteLast(head, 2);\r\n    printf(&quot;List after deletion of 4: &quot;);\r\n    display(head);\r\n    return 0;\r\n}\r\n\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4899_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n\r\n\/\/ A linked list Node\r\nclass Node {\r\n    public:\r\n    int data;\r\n    Node* next;\r\n};\r\n\r\nvoid deleteLast(Node* head, int X){\r\n    \/\/ Initialize special pointer with NULL\r\n    Node* special = NULL;\r\n\r\n    \/\/ Start from head and find the last Occurrence\r\n    \/\/ of node with value X\r\n    Node* temp = head;\r\n    while (temp) {\r\n        \/\/ If we found the key, update special\r\n        if (temp-&gt;data == X)\r\n            special = temp;\r\n\r\n        temp = temp-&gt;next;\r\n    }\r\n\r\n    \/\/ key occurs at-least once\r\n    if (special != NULL) {\r\n        Node* removeNode = head;\r\n        while(removeNode-&gt;next != special){\r\n            removeNode = removeNode-&gt;next;\r\n        }\r\n        removeNode-&gt;next = special-&gt;next;\r\n        special-&gt;next = NULL;\r\n        delete special;\r\n    }\r\n    \r\n}\r\n\r\n\/\/ function to create a new node with given data\r\nNode* newNode(int data){\r\n    Node* temp = new Node;\r\n    temp-&gt;data = data;\r\n    temp-&gt;next = NULL;\r\n    return temp;\r\n}\r\n\r\n\/\/ function to print the linked list\r\nvoid printList(Node* node){\r\n    while (node != NULL) {\r\n        cout&lt;&lt;node-&gt;data&lt;&lt;&quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\n\r\nint main(){\r\n    Node* head = newNode(1);\r\n    head-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next = newNode(7);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(5);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(2);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(10);\r\n\r\n    cout&lt;&lt;&quot;Input Linked List: &quot;;\r\n    printList(head);cout&lt;&lt;endl;\r\n    deleteLast(head, 2);\r\n    cout&lt;&lt;&quot;Output Linked List: &quot;;\r\n    printList(head);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4899_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass DeleteOccurances\r\n{\r\n\r\n\r\nstatic class Node\r\n{\r\n    int data;\r\n    Node next;\r\n};\r\n\r\n\/\/ Function to delete the last occurrence\r\nstatic void deleteLast(Node head, int x)\r\n{\r\n    Node temp = head, ptr = null;\r\n    while (temp!=null)\r\n    {\r\n\r\n        \/\/ If found key, update\r\n        if (temp.data == x)\r\n            ptr = temp;    \r\n        temp = temp.next;\r\n    }\r\n\r\n    \/\/ If the last occurrence is the last node\r\n    if (ptr != null &amp;&amp; ptr.next == null)\r\n    {\r\n        temp = head;\r\n        while (temp.next != ptr)\r\n            temp = temp.next;\r\n        temp.next = null;\r\n    }\r\n\r\n    \/\/ If it is not the last node\r\n    if (ptr != null &amp;&amp; ptr.next != null)\r\n    {\r\n        ptr.data = ptr.next.data;\r\n        temp = ptr.next;\r\n        ptr.next = ptr.next.next;\r\n        System.gc();\r\n    }\r\n}\r\nstatic Node newNode(int x)\r\n{\r\n    Node node = new Node();\r\n    node.data = x;\r\n    node.next = null;\r\n    return node;\r\n}\r\nstatic void display(Node head)\r\n{\r\n    Node temp = head;\r\n    if (head == null)\r\n    {\r\n        System.out.print(&quot;null&#92;n&quot;);\r\n        return;\r\n    }\r\n    while (temp != null)\r\n    {\r\n        System.out.printf(&quot;%d --&gt; &quot;, temp.data);\r\n        temp = temp.next;\r\n    }\r\n    System.out.print(&quot;null&#92;n&quot;);\r\n}\r\n\r\n\/* Driver code*\/\r\npublic static void main(String[] args)\r\n{\r\n    Node head = newNode(11);\r\n    head.next = newNode(2);\r\n    head.next.next = newNode(3);\r\n    head.next.next.next = newNode(4);\r\n    head.next.next.next.next = newNode(5);\r\n    head.next.next.next.next.next = newNode(4);\r\n    head.next.next.next.next.next.next = newNode(4);\r\n    System.out.print(&quot;Created Linked list: &quot;);\r\n    display(head);\r\n    deleteLast(head, 4);\r\n    System.out.print(&quot;List after deletion of 4: &quot;);\r\n    display(head);\r\n}\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4899_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n    def __init__(self, new_data):\r\n        \r\n        self.data = new_data\r\n        self.next = None\r\n\r\ndef deleteLast(head, x):\r\n\r\n    temp = head\r\n    ptr = None\r\n    \r\n    while (temp != None):\r\n\r\n        if (temp.data == x):\r\n            ptr = temp    \r\n        temp = temp.next\r\n    \r\n    if (ptr != None and ptr.next == None):\r\n        temp = head\r\n        while (temp.next != ptr):\r\n            temp = temp.next\r\n            \r\n        temp.next = None\r\n    \r\n    if (ptr != None and ptr.next != None):\r\n        ptr.data = ptr.next.data\r\n        temp = ptr.next\r\n        ptr.next = ptr.next.next\r\n        \r\n    return head\r\n    \r\ndef newNode(x):\r\n\r\n    node = Node(0)\r\n    node.data = x\r\n    node.next = None\r\n    return node\r\n\r\ndef display(head):\r\n\r\n    temp = head\r\n    if (head == None):\r\n        print(&quot;NULL&#92;n&quot;)\r\n        return\r\n    \r\n    while (temp != None):\r\n        print( temp.data, end = &quot; &quot;)\r\n        temp = temp.next\r\n    \r\n    print(&quot;NULL&quot;)\r\n\r\nhead = newNode(1)\r\nhead.next = newNode(2)\r\nhead.next.next = newNode(7)\r\nhead.next.next.next = newNode(5)\r\nhead.next.next.next.next = newNode(2)\r\nhead.next.next.next.next.next = newNode(10)\r\n\r\nprint(&quot;Created Linked list: &quot;, end = '')\r\ndisplay(head)\r\n\r\n# Pass the address of the head pointer\r\nhead = deleteLast(head, 2)\r\nprint(&quot;List after deletion of 4: &quot;, end = '')\r\n\r\ndisplay(head)\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4899 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4899 a\"),jQuery(\"#tab-content_4899\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>Input Linked List: 1 2 7 5 2 10<\/p>\n<p>Output Linked List: 1 2 7 5 10 <\/p>\n<p><strong>Time Complexity:<\/strong> O(N), Since we traversed through the list two times, One traversal for finding the last occurred node of value X in the linked list and another traversal for the deletion process.<\/p>\n<p><strong>Conclusion<\/strong><br \/>\nDeleting the last occurrence of an item in a linked list involves a nuanced approach that combines careful traversal and pointer manipulation. By understanding the intricacies of the linked list structure and employing appropriate algorithms, it&#8217;s possible to achieve this deletion efficiently. Throughout this article, we&#8217;ve explored different strategies and code implementations to accomplish this task. It&#8217;s important to choose a method that suits the specific context and requirements of the application, considering both time and space complexity. Mastering these techniques can enhance a programmer&#8217;s ability to work with linked lists and solve related challenges effectively.<\/p>\n<h2>FAQs (Frequently Asked Questions) Related to Delete Last Occurrence of an Item from the Linked List<\/h2>\n<p>Here are some FAQs (Frequently Asked Questions) Related to Delete Last Occurrence of an Item from the Linked List.<\/p>\n<p><strong>Q1: Why is deleting the last occurrence of an item in a linked list important?<\/strong><br \/>\nDeleting the last occurrence of an item in a linked list can be crucial for various applications, especially when maintaining a specific order or sequence of elements. For instance, in certain tasks related to data management or processing, removing the last instance of an item might impact the integrity or relevance of the information being processed.<\/p>\n<p><strong>Q2: Are there any particular challenges when deleting the last occurrence of an item?<\/strong><br \/>\nYes, there can be challenges, especially in scenarios where the linked list is large or when dealing with edge cases, such as the item being the only element in the list or being present multiple times consecutively at the end. Careful handling of pointers and edge case scenarios is essential for accurate deletion.<\/p>\n<p><strong>Q3: What are some common strategies to delete the last occurrence of an item in a linked list?<\/strong><br \/>\nOne approach involves traversing the linked list while maintaining pointers to track the last occurrence of the item and its preceding node. Another method includes utilizing recursion to locate the last instance and perform deletion. Selecting the appropriate strategy often depends on the specific requirements and constraints of the problem at hand.<\/p>\n<p><strong>Q4: How can I ensure the correctness of my implementation when deleting the last occurrence of an item?<\/strong><br \/>\nTesting your code extensively with various scenarios, including edge cases and different types of linked lists, is crucial. Validating the functionality through test cases and considering potential corner cases will help ensure the correctness and robustness of your implementation.<\/p>\n<table width=\"641\">\n<tbody>\n<tr>\n<td colspan=\"2\" width=\"641\" style=\"text-align: center\"><strong>Related Articles<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/doubly-circular-linked-list-introduction-and-insertion\/\">Doubly circular linked list in data structure<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantages-disadvantages-and-uses-of-a-doubly-linked-list\/\">Application of doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/applications-of-linked-list-data-structure\/\">Applications of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/difference-between-a-singly-linked-list-and-a-doubly-linked-list\/\">Singly linked list vs doubly linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/advantage-and-disadvantage-of-linked-list-over-array\/\">Advantages and disadvantages of linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/menu-driven-program-for-all-operations-on-doubly-linked-list-in-c\/\">Doubly linked list all operations in C<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/binary-search\/binary-search-on-linked-list\/\">Binary search in linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/bubble-sort-for-linked-list-by-swapping-nodes\/\">Bubble sort linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-in-doubly-linked-list\/\">Deletion in doubly linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-middle-of-linked-list\/\">Delete the middle node of a linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/adding-two-polynomials-using-linked-list\/\">Polynomial addition using linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-smallest-and-largest-elements-in-a-singly-linked-list\/\">Find max value and min value in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/insert-a-node-at-a-specific-position-in-a-linked-list\/\">Insert a node at a specific position in a linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/swap-nodes-in-a-linked-list-without-swapping-data\/\">Swap nodes in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/add-two-numbers-represented-by-linked-lists-set-1\/\">Add two numbers represented by linked lists<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/find-the-first-node-of-the-loop-in-a-linked-list\/\">Find starting point of loop in linked list<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/merge-sort-on-a-singly-linked-list\/\">Merge sort linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/delete-a-node-at-a-given-position\/\">Delete a node from linked list at a given position<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/remove-duplicates-from-an-unsorted-linked-list\/\">Remove duplicates from unsorted linked list<\/a><\/td>\n<td width=\"321\"><a href=\"https:\/\/prepbytes.com\/blog\/python\/python-program-to-reverse-a-linked-list\/\">Reverse a linked list in Python<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Linked lists are fundamental data structures in computer science, known for their flexibility and dynamic memory allocation. Deleting the last occurrence of a specific item within a linked list involves traversing the list to find the last instance of that item and then removing it while maintaining the list&#8217;s structural integrity. This operation requires careful [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4949","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Delete the last occurrence of an item from the linked list | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Delete the last occurrence of an item from the linked list | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-09-13T02:48:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-24T09:34:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Delete the last occurrence of an item from the linked list\",\"datePublished\":\"2021-09-13T02:48:17+00:00\",\"dateModified\":\"2023-11-24T09:34:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\"},\"wordCount\":1251,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\",\"name\":\"Delete the last occurrence of an item from the linked list | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\",\"datePublished\":\"2021-09-13T02:48:17+00:00\",\"dateModified\":\"2023-11-24T09:34:40+00:00\",\"description\":\"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Delete the last occurrence of an item from the linked list\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Delete the last occurrence of an item from the linked list | Linked List | Prepbytes","description":"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.","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:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Delete the last occurrence of an item from the linked list | Linked List | Prepbytes","og_description":"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.","og_url":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-13T02:48:17+00:00","article_modified_time":"2023-11-24T09:34:40+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Delete the last occurrence of an item from the linked list","datePublished":"2021-09-13T02:48:17+00:00","dateModified":"2023-11-24T09:34:40+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/"},"wordCount":1251,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/","name":"Delete the last occurrence of an item from the linked list | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png","datePublished":"2021-09-13T02:48:17+00:00","dateModified":"2023-11-24T09:34:40+00:00","description":"Learn how to search a linked list in another linked list. This blog explains how to delete all occurrences of a given key from a doubly linked list.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645000821527-Article_139.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/delete-the-last-occurrence-of-an-item-from-the-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Delete the last occurrence of an item from the linked list"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4949","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4949"}],"version-history":[{"count":9,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4949\/revisions"}],"predecessor-version":[{"id":18377,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4949\/revisions\/18377"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}