{"id":5047,"date":"2021-09-18T11:29:05","date_gmt":"2021-09-18T11:29:05","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5047"},"modified":"2022-11-04T05:59:59","modified_gmt":"2022-11-04T05:59:59","slug":"remove-duplicates-from-a-sorted-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/","title":{"rendered":"Remove duplicates from a sorted linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>We know that a linked list is a mutable data structure. And it might have duplicate elements in it. We basically have two types of linked lists: one is a sorted linked list and another is an unsorted linked list. Here we will see an approach to remove duplicates from sorted list<\/p>\n<\/p>\n<h3>Problem Statement on how to remove duplicates from sorted linked list<\/h3>\n<p>In this problem, we are given a sorted LinkedList and are asked to remove the duplicate elements present in the list.<\/p>\n<h3>Problem Statement Understanding on how to remove duplicates from sorted list.<\/h3>\n<p>According to the problem statement, we will be given a sorted linked list that may contain duplicate elements. We need to remove the duplicate elements from the list.<\/p>\n<p>Let&#8217;s try to understand the problem with the help of some examples.<\/p>\n<p>If the given input sorted linked list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/input-1-1.png\" alt=\"\" \/><\/p>\n<ul>\n<li>As we can see in the above list, there are multiple occurrences of elements 10, 12 in the linked list.<\/li>\n<li>So it means that the list contains duplicates of 10, 12 , and we need to remove these duplicates from the linked list.<\/li>\n<li>After removing the duplicate elements from the list, the output linked list will be:<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/output-13.png\" alt=\"\" \/><\/p>\n<p>If the linked list is: head-&gt;11-&gt;11-&gt;25-&gt;40-&gt;40-&gt;45.<\/p>\n<ul>\n<li>In this case, as we can see in the above list, 11 and 40 are duplicate elements present more than once in the list.<\/li>\n<li>After removing the duplicate elements from the list, the output will be head-&gt;11-&gt;25-&gt;40-&gt;45.<\/li>\n<\/ul>\n<h5>Some more examples:<\/h5>\n<p>Sample Input 1: head-&gt;2-&gt;2-&gt;3-&gt;4-&gt;7.<\/p>\n<p>Sample Output 1: head-&gt;2-&gt;3-&gt;4-&gt;7.<\/p>\n<p>Sample Input 2: head-&gt;3-&gt;3-&gt;7-&gt;7-&gt;7-&gt;11-&gt;11-&gt;12-&gt;15.<\/p>\n<p>Sample Input 2: head-&gt;3-&gt;7-&gt;11-&gt;12-&gt;15.<\/p>\n<p>Now, I think from the above examples, 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 1 (Using Hashmaps) on how to remove duplicates from sorted list<\/h3>\n<p>Our approach will be simple; we will be using hashmap:<\/p>\n<ul>\n<li>While traversing the Linked List, for every element, we will check if it is already present in the map or not.\n<ul>\n<li>If it is already present, then we will remove the element from the list. <\/li>\n<li>Else we will push the element in the hash map and move forward. <\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>This approach will require extra space.<\/p>\n<p><strong>Time complexity<\/strong>: O(n), where n is the number of nodes in the linked list.<\/p>\n<p><strong>Space complexity<\/strong>: O(n), where n is the number of nodes in the linked list.<\/p>\n<p>Although the above approach will work fine, but it requires some extra space. Now the main question is do we necessarily need this extra space? Can we remove duplicates from the sorted linked list without consuming extra space?<\/p>\n<ul>\n<li>Let&#8217;s see in the next section how we can solve this problem without using extra space.<\/li>\n<\/ul>\n<p>Let&#8217;s move to the next approach.<\/p>\n<h3>Approach 2 (Using two pointers)on how to remove duplicates from sorted linked list<\/h3>\n<p>Our approach will be straightforward:<\/p>\n<ul>\n<li>We will create a pointer <strong>prev<\/strong> that will to the first occurrence of the element and second pointer <strong>temp<\/strong> to traverse the linked list. <\/li>\n<li>If the value of the <strong>temp<\/strong> pointer is equal to the <strong>prev<\/strong> pointer, temp will move forward and when we find the node whose value is not equal to prev pointer, we will connect the node pointer by <strong>prev<\/strong> with the node pointed by <strong>temp<\/strong> and move <strong>prev<\/strong> pointer to the position of <strong>temp<\/strong> and move <strong>temp<\/strong> forward.<\/li>\n<\/ul>\n<p>From the above approach, we can see that we are using constant extra space here.<\/p>\n<p><strong>Time complexity<\/strong>: O(n), where n is the number of nodes in the linked list.<\/p>\n<p><strong>Space complexity<\/strong>: O(1), As constant extra space, is required.<\/p>\n<p>Let&#8217;s see another approach that solves this problem in similar time and space complexity using two pointers for the sake of learning.<\/p>\n<h3>Approach 3<\/h3>\n<p>The idea is very simple:<\/p>\n<ul>\n<li>Traverse the list using a pointer, say <strong>current<\/strong>.<\/li>\n<li>While traversing the list compare the current node&#8217;s value with the value of it&#8217;s next node.\n<ul>\n<li>If they are the same, remove the next node. <\/li>\n<li>Else move forward. <\/li>\n<\/ul>\n<\/li>\n<li>Keep on doing this till we reach the end of the linked list.<\/li>\n<\/ul>\n<h3>Algorithm on how to remove duplicates from sorted list<\/h3>\n<ul>\n<li>Create a pointer <strong>current<\/strong>.<\/li>\n<li>Traverse the Linked List in a linear fashion.<\/li>\n<li>For every node, check the next node&#8217;s data.\n<ul>\n<li>If they are the same, then delete the current&#8217;s next node by changing the <strong>current<\/strong> node pointer.<\/li>\n<li>If they are different, then move <strong>current<\/strong> pointer forward <strong>(current = current-&gt;next)<\/strong>.<\/li>\n<\/ul>\n<\/li>\n<li>Keep on doing this until <strong>current-&gt;next != NULL<\/strong> or we can say until we reach the end of the linked list.<\/li>\n<\/ul>\n<h3>Dry Run on how to remove duplicates from sorted linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-9.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-9.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation on how to remove duplicates from sorted 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_5049 {\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_5049 .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_5049 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5049 .wpsm_nav-tabs > li.active > a, #tab_container_5049 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5049 .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_5049 .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_5049 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5049 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5049 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5049 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5049 .wpsm_nav-tabs > li > a:hover , #tab_container_5049 .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_5049 .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_5049 .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_5049 .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_5049 .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_5049 .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_5049 .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_5049 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5049 .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_5049 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5049 .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_5049 .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_5049\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5049\">\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_5049_1\" aria-controls=\"tabs_desc_5049_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_5049_2\" aria-controls=\"tabs_desc_5049_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_5049_3\" aria-controls=\"tabs_desc_5049_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_5049_4\" aria-controls=\"tabs_desc_5049_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_5049\">\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_5049_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\/* Link list node *\/\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/* The function removes duplicates from a sorted list *\/\r\nvoid removeDuplicates(struct Node* head)\r\n{\r\n    \/* Pointer to traverse the linked list *\/\r\n    struct Node* current = head;\r\n \r\n    \/* Pointer to store the next pointer of a node to be deleted*\/\r\n    struct Node* next_next;\r\n   \r\n    \/* do nothing if the list is empty *\/\r\n    if (current == NULL)\r\n       return;\r\n \r\n    \/* Traverse the list till last node *\/\r\n    while (current-&gt;next != NULL)\r\n    {\r\n       \/* Compare current node with next node *\/\r\n       if (current-&gt;data == current-&gt;next-&gt;data)\r\n       {\r\n           \/* The sequence of steps is important*\/              \r\n           next_next = current-&gt;next-&gt;next;\r\n           free(current-&gt;next);\r\n           current-&gt;next = next_next; \r\n       }\r\n       else \/* This is tricky: only advance if no deletion *\/\r\n       {\r\n          current = current-&gt;next;\r\n       }\r\n    }\r\n}\r\n \r\n\/* UTILITY FUNCTIONS *\/\r\n\/* Function to insert a node at the beginning of the linked list *\/\r\nvoid push(struct Node** head_ref, int new_data)\r\n{\r\n    \/* allocate node *\/\r\n    struct Node* new_node =\r\n            (struct Node*) malloc(sizeof(struct Node));\r\n             \r\n    \/* put in the data  *\/\r\n    new_node-&gt;data  = new_data;\r\n                 \r\n    \/* link the old list off the new node *\/\r\n    new_node-&gt;next = (*head_ref);    \r\n         \r\n    \/* move the head to point to the new node *\/\r\n    (*head_ref)    = new_node;\r\n}\r\n \r\n\/* Function to print nodes in a given linked list *\/\r\nvoid printList(struct Node *node)\r\n{\r\n    while (node!=NULL)\r\n    {\r\n       printf(&quot;%d &quot;, node-&gt;data);\r\n       node = node-&gt;next;\r\n    }\r\n}\r\n \r\n\/* Driver program to test above functions*\/\r\nint main()\r\n{\r\n    \/* Start with the empty list *\/\r\n    struct Node* head = NULL;\r\n   \r\n    \/* Let us create a sorted linked list to test the functions\r\n     Created linked list will be 11-&gt;11-&gt;11-&gt;13-&gt;13-&gt;20 *\/\r\n    push(&amp;head, 20);\r\n    push(&amp;head, 130);\r\n    push(&amp;head, 130); \r\n    push(&amp;head, 110);\r\n    push(&amp;head, 110);\r\n    push(&amp;head, 110);                                   \r\n \r\n    printf(&quot;&#92;n Linked list before duplicate removal  &quot;);\r\n    printList(head);\r\n \r\n    \/* Remove duplicates from linked list *\/\r\n    removeDuplicates(head);\r\n \r\n    printf(&quot;&#92;n Linked list after duplicate removal &quot;);        \r\n    printList(head);           \r\n   \r\n    return 0;\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_5049_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\/* Node structure of the linked list nodes *\/\r\nclass Node{\r\n\tpublic:\r\n\tint data;\r\n\tNode* next;\r\n};\r\n\r\n\/* Using this function we will be removing the duplicates elements from the given sorted linked list *\/\r\nvoid removeDuplicatesSortedList(Node* head){\r\n\tNode* current = head;\r\n\tNode* next_next;\r\n\tif (current == NULL)\r\n\treturn;\r\n\twhile (current-&gt;next != NULL){\r\n\t\tif (current-&gt;data == current-&gt;next-&gt;data){\r\n\t\t\tnext_next = current-&gt;next-&gt;next;\r\n\t\t\tfree(current-&gt;next); \r\n\t\t\tcurrent-&gt;next = next_next;\r\n\t\t}\r\n\t\telse{\r\n\t\t\tcurrent = current-&gt;next;\r\n\t\t}\r\n\t}\r\n}\r\n\r\n\/* Using this function we will be pushing a new node at the head of the linked list *\/\r\nvoid pushNodeHead(Node** head, int data){\r\n\tNode* new_node = new Node();\r\n\tnew_node-&gt;data = data;\r\n\tnew_node-&gt;next = (*head);\t\r\n\t(*head) = new_node;\r\n}\r\n\r\n\/* Using this function we will be printing the linked list *\/\r\nvoid printingLinkedList(Node *node){\r\n\twhile (node!=NULL){\r\n\t\tcout&lt;&lt;&quot; &quot;&lt;&lt;node-&gt;data;\r\n\t\tnode = node-&gt;next;\r\n\t}\r\n\tcout&lt;&lt;endl;\r\n}\r\n\r\nint main()\r\n{\r\n\tNode* head = NULL;\r\n\tpushNodeHead(&amp;head, 35);\r\n\tpushNodeHead(&amp;head, 30);\r\n\tpushNodeHead(&amp;head, 12);\r\n\tpushNodeHead(&amp;head, 10);\r\n\tpushNodeHead(&amp;head, 10);\r\n\tpushNodeHead(&amp;head, 10);\r\n\r\n\tcout&lt;&lt;&quot;Linked list before removing duplicates&quot;&lt;&lt;endl;\r\n\tprintingLinkedList(head);\r\n\r\n\tremoveDuplicatesSortedList(head);\r\n\r\n\tcout&lt;&lt;&quot;Linked list after removing duplicates&quot;&lt;&lt;endl;\r\n\tprintingLinkedList(head);\t\t\t\r\n\treturn 0;\r\n}\r\n\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_5049_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\n\r\nclass DeleteDuplicates\r\n{\r\n    Node head; \r\n    class Node\r\n    {\r\n        int data;\r\n        Node next;\r\n        Node(int d) {data = d; next = null; }\r\n    }\r\n\r\n    void removeDuplicates()\r\n    {\r\n        Node curr = head;\r\n\r\n        \/* Traverse list till the last node *\/\r\n        while (curr != null) {\r\n            Node temp = curr;\r\n            \/*Compare current node with the next node and\r\n            keep on deleting them until it matches the current\r\n            node data *\/\r\n            while(temp!=null &amp;&amp; temp.data==curr.data) {\r\n                temp = temp.next;\r\n            }\r\n            \/*Set current node next to the next different\r\n            element denoted by temp*\/\r\n            curr.next = temp;\r\n            curr = curr.next;\r\n        }\r\n    }\r\n    void push(int new_data)\r\n    {\r\n        Node new_node = new Node(new_data);\r\n        new_node.next = head;\r\n        head = new_node;\r\n    }\r\n    void printList()\r\n    {\r\n        Node temp = head;\r\n        while (temp != null)\r\n        {\r\n            System.out.print(temp.data+&quot; &quot;);\r\n            temp = temp.next;\r\n        }\r\n        System.out.println();\r\n    }\r\n    public static void main(String args[])\r\n    {\r\n        DeleteDuplicates llist = new DeleteDuplicates();\r\n        llist.push(20);\r\n        llist.push(13);\r\n        llist.push(13);\r\n        llist.push(11);\r\n        llist.push(11);\r\n        llist.push(11);\r\n        \r\n        System.out.println(&quot;List before removal of duplicates&quot;);\r\n        llist.printList();\r\n        \r\n        llist.removeDuplicates();\r\n        \r\n        System.out.println(&quot;List after removal of elements&quot;);\r\n        llist.printList();\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_5049_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\r\n    def __init__(self, data):\r\n        self.data = data\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\r\n    def deleteNode(self, key):\r\n        \r\n        temp = self.head\r\n\r\n        if (temp is not None):\r\n            if (temp.data == key):\r\n                self.head = temp.next\r\n                temp = None\r\n                return\r\n\r\n        while(temp is not None):\r\n            if temp.data == key:\r\n                break\r\n            prev = temp\r\n            temp = temp.next\r\n\r\n        if(temp == None):\r\n            return\r\n\r\n        prev.next = temp.next\r\n        temp = None\r\n\r\n    def printList(self):\r\n        temp = self.head\r\n        while(temp):\r\n            print(temp.data , end = ' ')\r\n            temp = temp.next\r\n    \r\n    def removeDuplicates(self):\r\n        temp = self.head\r\n        if temp is None:\r\n            return\r\n        while temp.next is not None:\r\n            if temp.data == temp.next.data:\r\n                new = temp.next.next\r\n                temp.next = None\r\n                temp.next = new\r\n            else:\r\n                temp = temp.next\r\n        return self.head\r\n\r\nllist = LinkedList()\r\n\r\nllist.push(35)\r\nllist.push(30)\r\nllist.push(12)\r\nllist.push(10)\r\nllist.push(10)\r\nllist.push(10)\r\nprint (&quot;Created Linked List: &quot;)\r\nllist.printList()\r\nprint()\r\nprint(&quot;Linked List after removing&quot;,\r\n            &quot;duplicate elements:&quot;)\r\nllist.removeDuplicates()\r\nllist.printList()\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_5049 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_5049 a\"),jQuery(\"#tab-content_5049\"));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>Linked list before removing duplicates<\/p>\n<p>10 10 10 12 30 35<\/p>\n<p>Linked list after removing duplicates<\/p>\n<p>10 12 30 35<\/p>\n<p><strong>Time Complexity<\/strong>: O(n), single traversal of the list require O(n) time, where n is the number of nodes in the linked list.<\/p>\n<h3> Conclusion<\/h3>\n<p>In this blog, we have seen an approach on how to remove duplicates from sorted list in linear time and haven\u2019t used extra space too. We have also seen the traversing through the linked list in linear fashion.we have understood different types of approaches, dry run and code implementations on how to remove duplicates from sorted linked list. This is a basic problem and is good for strengthening your concepts in LinkedList and if you want to practice more such problems, you can checkout <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Prepbytes (Linked List)<\/a>.<\/p>\n<h2> FAQs on how to remove duplicates from sorted list.<\/h2>\n<ol>\n<li><strong>What are the approaches which are used to remove duplicates from sorted list? <\/strong><\/li>\n<p>There are two known approaches : <\/p>\n<ul>\n<li>Approach -1 using Hashmaps.<\/li>\n<li>Approach -2 using pointers.<\/li>\n<\/ul>\n<li><strong>What is the time complexity to remove duplicates from sorted list?<\/strong><\/li>\n<p>The time complexity to remove duplicates from sorted list is O(n).<\/p>\n<li><strong>How is the data stored in the linked list?<\/strong><\/li>\n<p>Each element in the linked list is stored in the form of a node where node is a collection of two parts in which one part is a data part which has a value and the next part is a pointer which stores the address of the next node. <\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Introduction We know that a linked list is a mutable data structure. And it might have duplicate elements in it. We basically have two types of linked lists: one is a sorted linked list and another is an unsorted linked list. Here we will see an approach to remove duplicates from sorted list Problem Statement [&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-5047","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 duplicates from a sorted linked list | Linked list articles | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"Learn how to remove duplicate elements from a sorted linked list when the root node is given.\" \/>\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-duplicates-from-a-sorted-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Remove duplicates from a sorted linked list | Linked list articles | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"Learn how to remove duplicate elements from a sorted linked list when the root node is given.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-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-18T11:29:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-04T05:59:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.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\/remove-duplicates-from-a-sorted-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Remove duplicates from a sorted linked list\",\"datePublished\":\"2021-09-18T11:29:05+00:00\",\"dateModified\":\"2022-11-04T05:59:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/\"},\"wordCount\":1116,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/\",\"name\":\"Remove duplicates from a sorted linked list | Linked list articles | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png\",\"datePublished\":\"2021-09-18T11:29:05+00:00\",\"dateModified\":\"2022-11-04T05:59:59+00:00\",\"description\":\"Learn how to remove duplicate elements from a sorted linked list when the root node is given.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-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\":\"Remove 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\/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":"Remove duplicates from a sorted linked list | Linked list articles | PrepBytes Blog","description":"Learn how to remove duplicate elements from a sorted linked list when the root node is given.","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-duplicates-from-a-sorted-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Remove duplicates from a sorted linked list | Linked list articles | PrepBytes Blog","og_description":"Learn how to remove duplicate elements from a sorted linked list when the root node is given.","og_url":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-18T11:29:05+00:00","article_modified_time":"2022-11-04T05:59:59+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.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\/remove-duplicates-from-a-sorted-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Remove duplicates from a sorted linked list","datePublished":"2021-09-18T11:29:05+00:00","dateModified":"2022-11-04T05:59:59+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/"},"wordCount":1116,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/","name":"Remove duplicates from a sorted linked list | Linked list articles | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png","datePublished":"2021-09-18T11:29:05+00:00","dateModified":"2022-11-04T05:59:59+00:00","description":"Learn how to remove duplicate elements from a sorted linked list when the root node is given.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001388042-Article_150.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-a-sorted-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":"Remove 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\/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\/5047","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=5047"}],"version-history":[{"count":6,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions"}],"predecessor-version":[{"id":10324,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5047\/revisions\/10324"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}