{"id":3858,"date":"2021-08-16T13:05:20","date_gmt":"2021-08-16T13:05:20","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=3858"},"modified":"2022-11-18T07:14:15","modified_gmt":"2022-11-18T07:14:15","slug":"remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/","title":{"rendered":"Remove all occurrences of duplicates from a sorted linked list."},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png\" alt=\"\" \/><\/p>\n<p>We have observed that a linked list might contain elements which repeat themselves in the list. So, now we will look into a program to remove all occurrences of duplicates from a sorted linked list\n <\/p>\n<h3>Problem Statement to remove all occurrences of duplicates from a sorted linked list<\/h3>\n<p>In this question, we are given a sorted Singly Linked List. We have to remove all the occurrences of duplicate nodes in the list.<\/p>\n<h3>Problem Statement Understanding on how to remove all occurrences of duplicates from a sorted linked list<\/h3>\n<p>Let\u2019s try to understand the problem statement with examples.<\/p>\n<p>Suppose if the given sorted list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-1-3.png\" alt=\"\" \/><\/p>\n<p>Now, we have to remove all the occurrences of duplicates from this list. As we can see, 39 is the only repeating value (which have more than 1 occurrences in the linked list), so we will delete all the nodes which have the value 39.<\/p>\n<p>So, the final linked list after deletion:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/final-linked-list-1-2.png\" alt=\"\" \/><\/p>\n<p>If the given sorted list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-2-3.png\" alt=\"\" \/><\/p>\n<p>Now, as we can see that 2 and 3 are the only repeating values in the linked list (which have more than 1 occurrences in the linked list) so we will have to remove all the occurrences of 2 and 3 from this list. <\/p>\n<p>So, the final linked list after deletion:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/final-linked-list-2-2.png\" alt=\"\" \/><\/p>\n<p><strong>Explanation:<\/strong> All the nodes which appear more than once have been deleted from the input linked list. The only nodes which appear exactly once in the list remain in the list.<\/p>\n<p>This question is not a very complex one. We have to make use of list traversal in the question. Let us have a glance at the approach.<\/p>\n<p>Before moving to the approach, just try to think how you can solve this problem?<\/p>\n<h3>Approach on how to remove all occurrences of duplicates from a sorted linked list<\/h3>\n<p>The approach is going to be pretty simple. We will maintain a previous pointer which will point to the node just previous to the block of nodes for which we will check for the duplicates, i.e. the previous pointer will point to the last node which has no duplicate.<\/p>\n<p>As long as we will keep finding duplicates, we will keep traversing through the list. As soon as the duplicates end, we will make the previous pointer point to the next of the last duplicate node of that block. Let us have a look at the algorithm.<\/p>\n<h3>Algorithm for how to remove all occurrences of duplicates from a sorted linked list<\/h3>\n<ul>\n<li>Create a node dummy which will point to the head of the list, and another node previous which will point to the last node which has no duplicate (initially previous will be pointing to dummy).<\/li>\n<li>Create another node that will be used to traverse through the list. Let us call this node current.<\/li>\n<li>Traverse using current till the end of the list.<\/li>\n<li>Now, as long as the data of next of previous is equal to the data of next of current,  increment current by 1.<\/li>\n<li>If the current stays the same, i.e. no duplicate in the current block, then <strong>previous = previous &#8211; &gt; next<\/strong>.  By doing this, we are just incrementing the previous.<\/li>\n<li>Else, there are duplicates, so, <strong>previous &#8211; &gt; next = current &#8211; &gt; next<\/strong>. The reason for doing this is that the previous is pointing to the last node which has no duplicates, and the current has been increased, which means there are duplicates. So, we will make the previous point to the next of current in order to remove the duplicates.<\/li>\n<li>Increment current by one.<\/li>\n<li>After the traversal, make head point to the next of the dummy node.<\/li>\n<\/ul>\n<h3>Dry Run on how to remove all occurrences of duplicates from a sorted linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Remove-all-occurrence-of-duplicates-from-a-sorted-linked-list-2.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_3863 {\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_3863 .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_3863 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_3863 .wpsm_nav-tabs > li.active > a, #tab_container_3863 .wpsm_nav-tabs > li.active > a:hover, #tab_container_3863 .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_3863 .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_3863 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_3863 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_3863 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_3863 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_3863 .wpsm_nav-tabs > li > a:hover , #tab_container_3863 .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_3863 .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_3863 .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_3863 .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_3863 .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_3863 .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_3863 .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_3863 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3863 .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_3863 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_3863 .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_3863 .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_3863\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_3863\">\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_3863_1\" aria-controls=\"tabs_desc_3863_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_3863_2\" aria-controls=\"tabs_desc_3863_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_3863_3\" aria-controls=\"tabs_desc_3863_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_3863_4\" aria-controls=\"tabs_desc_3863_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_3863\">\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_3863_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 <stdio.h>\r\n#include <stdlib.h>\r\n \r\n\/\/ A Linked List Node\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ Helper function to print a given linked list\r\nvoid printList(struct Node* head)\r\n{\r\n    struct Node* ptr = head;\r\n    while (ptr)\r\n    {\r\n        printf(\"%d \u2014> \", ptr->data);\r\n        ptr = ptr->next;\r\n    }\r\n \r\n    \/\/printf(\"NULL\");\r\n}\r\n \r\n\/\/ Helper function to insert a new node at the beginning of the linked list\r\nvoid push(struct Node** head, int data)\r\n{\r\n    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));\r\n    newNode->data = data;\r\n    newNode->next = *head;\r\n    *head = newNode;\r\n}\r\n \r\n\/\/ Remove duplicates from a sorted list\r\nvoid removeDuplicates(struct Node* head)\r\n{\r\n    \/\/ do nothing if the list is empty\r\n    if (head == NULL) {\r\n        return;\r\n    }\r\n \r\n    struct Node* current = head;\r\n \r\n    \/\/ compare the current node with the next node\r\n    while (current->next != NULL)\r\n    {\r\n        if (current->data == current->next->data)\r\n        {\r\n            struct Node* nextNext = current->next->next;\r\n            free(current->next);\r\n            current->next = nextNext;\r\n        }\r\n        else {\r\n            current = current->next;    \/\/ only advance if no deletion\r\n        }\r\n    }\r\n}\r\n \r\n\/\/ Driver Code\r\nint main()\r\n{\r\n    \/\/ 23->28->28->35->49->49->53->53\r\n    struct Node* head = NULL;\r\n \r\n    \/\/ create linked list 10->12->8->4->6\r\n    push(&head, 16);\r\n    push(&head, 16);\r\n    push(&head, 80);\r\n    push(&head, 12);\r\n    push(&head, 20);\r\n    printf(\"List before removal of duplicates&#92;n\");\r\n    printList(head);\r\n    printf(\"&#92;nList After removal of duplicates&#92;n\");\r\n    removeDuplicates(head);\r\n \r\n    \/\/ print linked list\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_3863_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 <bits stdc++.h=\"\">\r\nusing namespace std;\r\n \r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node *next;\r\n};\r\n\r\nstruct Node *newNode(int data)\r\n{\r\n    Node *temp = new Node;\r\n    temp -> data = data;\r\n    temp -> next = NULL;\r\n    return temp;\r\n}\r\n\r\nvoid printList(struct Node *node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        printf(\"%d \", node -> data);\r\n        node = node -> next;\r\n    }\r\n}\r\n\r\nvoid removeAllDuplicates(struct Node* &start)\r\n{\r\n\r\n    Node* dummy = new Node;\r\n\r\n    dummy -> next = start;\r\n\r\n    Node* previous = dummy;\r\n\r\n    Node* current = start;\r\n \r\n    while(current != NULL)\r\n    {\r\n\r\n        while(current -> next != NULL &&\r\n              previous -> next -> data == current -> next -> data)\r\n            current = current -> next;\r\n\r\n        if (previous -> next == current)\r\n            previous = previous -> next;\r\n\r\n        else\r\n            previous -> next = current -> next;\r\n \r\n        current = current -> next;\r\n    }\r\n \r\n    \/\/ update original head to\r\n    \/\/ the next of dummy node\r\n    start = dummy -> next;\r\n}\r\n \r\n\/\/ Driver Code\r\nint main()\r\n{\r\n\r\n    struct Node* start = newNode(23);\r\n    start -> next = newNode(29);\r\n    start -> next -> next = newNode(39);\r\n    start -> next -> next -> next = newNode(39);\r\n    start -> next -> next -> next -> next = newNode(44);\r\n    \r\n    cout << \"List before removal \" <<\r\n                  \"of duplicates&#92;n\";\r\n    printList(start);\r\n     \r\n    removeAllDuplicates(start);\r\n     \r\n    cout << \"&#92;nList after removal \" <<\r\n                   \"of duplicates&#92;n\";\r\n    printList(start);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\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_3863_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\npublic class LinkedList{\r\n\r\nNode head = null;\r\n\r\nclass Node\r\n{\r\n    int val;\r\n    Node next;\r\n    Node(int v)\r\n    {\r\n\r\n        val = v;\r\n        next = null;\r\n    }\r\n}\r\n\r\npublic void insert(int data)\r\n{\r\n    Node new_node = new Node(data);\r\n    new_node.next = head;\r\n    head = new_node;\r\n}\r\n\r\npublic void removeAllDuplicates()\r\n{\r\n\r\n    Node dummy = new Node(0);\r\n\r\n\r\n    dummy.next = head;\r\n    Node prev = dummy;\r\n    Node current = head;\r\n\r\n    while (current != null)\r\n    {\r\n\r\n        while (current.next != null &&\r\n            prev.next.val == current.next.val)\r\n            current = current.next;\r\n\r\n        if (prev.next == current)\r\n            prev = prev.next;\r\n\r\n        else\r\n            prev.next = current.next;\r\n\r\n        current = current.next;\r\n    }\r\n\r\n    head = dummy.next;\r\n}\r\n\r\npublic void printList()\r\n{\r\n    Node trav = head;\r\n    if (head == null)\r\n        System.out.print(\" List is empty\" );\r\n        \r\n    while (trav != null)\r\n    {\r\n        System.out.print(trav.val + \" \");\r\n        trav = trav.next;\r\n    }\r\n}\r\n\r\n\r\npublic static void main(String[] args)\r\n{\r\n    LinkedList ll = new LinkedList();\r\n\r\n    ll.insert(44);\r\n    ll.insert(39);\r\n    ll.insert(39);\r\n    ll.insert(29);\r\n    ll.insert(23);\r\n    \r\n    System.out.println(\"Before removal of duplicates\");\r\n    ll.printList();\r\n\r\n    ll.removeAllDuplicates();\r\n\r\n    System.out.println(\"&#92;nAfter removal of duplicates\");\r\n    ll.printList();\r\n}\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_3863_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, val):\r\n        self.val = val\r\n        self.next = None\r\n\r\nclass LinkedList:\r\n\r\n    def __init__(self):\r\n        self.head = None\r\n        \r\n    def push(self, new_data):\r\n        new_node = Node(new_data)\r\n        new_node.next = self.head\r\n        self.head = new_node\r\n        return new_node\r\n        \r\n    def removeAllDuplicates(self, temp):\r\n        \r\n        curr = temp\r\n        head = prev = Node(None)\r\n        head.next = curr\r\n        while curr and curr.next:\r\n            \r\n            if curr.val == curr.next.val:\r\n                while(curr and curr.next and\r\n                    curr.val == curr.next.val):\r\n                    curr = curr.next\r\n                    \r\n                curr = curr.next\r\n                prev.next = curr\r\n            else:\r\n                prev = prev.next\r\n                curr = curr.next\r\n        return head.next\r\n        \r\n    def printList(self):\r\n        temp1 = self.head\r\n        while temp1 is not None:\r\n            print(temp1.val, end = \" \")\r\n            temp1 = temp1.next\r\n            \r\n    def get_head(self):\r\n        return self.head\r\n\r\nif __name__=='__main__':\r\n    llist = LinkedList()\r\n    llist.push(44)\r\n    llist.push(39)\r\n    llist.push(39)\r\n    llist.push(29)\r\n    llist.push(23)\r\n    \r\n    print('Created linked list is:')\r\n    llist.printList()\r\n    print('&#92;nLinked list after deletion of duplicates:')\r\n    head1 = llist.get_head()\r\n    llist.removeAllDuplicates(head1)\r\n    llist.printList()\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\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_3863 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_3863 a\"),jQuery(\"#tab-content_3863\"));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<h3>Output<\/h3>\n<p>List before removal of duplicates<br \/>\n23 29 39 39 44<br \/>\nList after removal of duplicates<br \/>\n23 29 44<\/p>\n<p>\n<strong>Space Complexity:<\/strong> O(1), as only temporary variables are being created.<\/p>\n<h3> Conclusion <\/h3>\n<p>So, in the below article, we have seen how duplicates occur in the linked list and how we remove all occurrences of duplicates from a sorted linked list. It looks like a nested traversal, but it is not. This is an important question when it comes to coding interviews. If you want to solve more questions on Linked List, which are curated by our expert mentors at PrepBytes, you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/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<h2> FAQs <\/h2>\n<p><ol><strong><\/p>\n<li>What function can remove duplicates? <\/li>\n<p><\/strong><br \/>\nUsing sort() we can remove duplicates in a list.<br \/>\n<strong><\/p>\n<li>Which function removes duplicate values from an array? <\/li>\n<p><\/strong><br \/>\nUsing filter()which creates a new array which have to pass the condition.Accordingly it will return elements.<br \/>\n<strong><\/p>\n<li>Can we use Hashset to remove duplicates?<\/li>\n<p><\/strong><br \/>\nBasically hashset does not allow any duplicate elements in the list. So, a hashset can be helpful in removing all duplicate values. <\/ol><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have observed that a linked list might contain elements which repeat themselves in the list. So, now we will look into a program to remove all occurrences of duplicates from a sorted linked list Problem Statement to remove all occurrences of duplicates from a sorted linked list In this question, we are given a [&hellip;]<\/p>\n","protected":false},"author":52,"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-3858","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>Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted 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\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted linked list\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\" \/>\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-08-16T13:05:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-18T07:14:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.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=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Remove all occurrences of duplicates from a sorted linked list.\",\"datePublished\":\"2021-08-16T13:05:20+00:00\",\"dateModified\":\"2022-11-18T07:14:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\"},\"wordCount\":923,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\",\"name\":\"Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png\",\"datePublished\":\"2021-08-16T13:05:20+00:00\",\"dateModified\":\"2022-11-18T07:14:15+00:00\",\"description\":\"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted linked list\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#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\":\"Remove all occurrences of duplicates from a sorted 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\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes","description":"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted 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\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/","og_locale":"en_US","og_type":"article","og_title":"Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes","og_description":"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted linked list","og_url":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-16T13:05:20+00:00","article_modified_time":"2022-11-18T07:14:15+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Remove all occurrences of duplicates from a sorted linked list.","datePublished":"2021-08-16T13:05:20+00:00","dateModified":"2022-11-18T07:14:15+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/"},"wordCount":923,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/","url":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/","name":"Remove all occurrences of duplicates from a sorted linked list | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png","datePublished":"2021-08-16T13:05:20+00:00","dateModified":"2022-11-18T07:14:15+00:00","description":"Learn the most efficient way to remove all occurrences of duplicates from a sorted linked list. This article explains the approach to delete all occurrences of duplicates from a sorted linked list","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1644925835076-103.Remove%20all%20occurrences%20of%20duplicates-04.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/remove-all-occurrences-of-duplicates-from-a-sorted-linked-list-2\/#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":"Remove all occurrences of duplicates from a sorted 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\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3858","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\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=3858"}],"version-history":[{"count":8,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3858\/revisions"}],"predecessor-version":[{"id":10577,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/3858\/revisions\/10577"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=3858"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=3858"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=3858"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}