{"id":5043,"date":"2021-09-18T11:19:17","date_gmt":"2021-09-18T11:19:17","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=5043"},"modified":"2023-07-27T12:20:23","modified_gmt":"2023-07-27T12:20:23","slug":"remove-duplicates-from-an-unsorted-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/","title":{"rendered":"Remove duplicates from an unsorted linked list"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png\" alt=\"\" \/><\/p>\n<p>Although linked lists can store unique values, there are situations where duplicates can be present. Hence, the focus of this article is to explain how to remove duplicates from unsorted linked lists.<\/p>\n<h2>How to Remove Duplicates from Unsorted Linked List<\/h2>\n<p>In this problem, we are given an unsorted LinkedList and are asked to remove the duplicate elements present in the list.<\/p>\n<p>As per the given problem statement, we are presented with an unsorted linked list that potentially includes duplicate elements. Our objective is to eliminate these duplicates from the unsorted linked 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 unsorted linked list is:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/input-14.png\" alt=\"\" \/><\/p>\n<ul>\n<li>As we can see in the above list, there are multiple occurrences of elements 10, 12 and 11 in the linked list.<\/li>\n<li>So it means that the list contain duplicates of 10, 12 and 11, 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-12.png\" alt=\"\" \/><\/p>\n<p>If the linked list is: head-&gt;10-&gt;12-&gt;12-&gt;15-&gt;10-&gt;20-&gt;20.<\/p>\n<ul>\n<li>In this case, as we can see in the above list, 10, 12 and 20 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;10-&gt;12-&gt;15-&gt;20.<\/li>\n<\/ul>\n<p><Strong>Some more examples:<\/Strong><\/p>\n<p>Sample Input 1: head-&gt;2-&gt;3-&gt;2-&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;9-&gt;7-&gt;11-&gt;4-&gt;9-&gt;15.<\/p>\n<p>Sample Input 2: head-&gt;3-&gt;7-&gt;9-&gt;11-&gt;4-&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 two loops) to remove duplicates from unsorted linked list.<\/h3>\n<p>Duplicate elements can be removed using two loops:<\/p>\n<ul>\n<li>Outer loop for traversing the linked list and picking the element of list one by one and inner loop to check if in the rest of the list (starting from outer loop pointer&#8217;s next to the end of the list) any duplicate of the element picked up by outer loop is present or not. <\/li>\n<li>If the duplicate is present in the linked list, remove the duplicate element from the list.<\/li>\n<\/ul>\n<p>The <strong>time complexity<\/strong> for this approach will be O(n<sup>2<\/sup>). <\/p>\n<ul>\n<li>Although the above approach will work fine but if in case the length of our linked list is greater than or equal to 10<sup>4<\/sup>, it will give us time limit exceed error (TLE).<\/li>\n<\/ul>\n<p>So to solve the above problem of TLE, we will see in the next approach how using sorting can solve our problem without getting TLE.<\/p>\n<h3>Approach 2 (Using merge sort)to remove duplicates from unsorted linked list.<\/h3>\n<p>Usually, merge sort is used to sort the linked lists.<\/p>\n<ul>\n<li>We will sort the linked list by using merge sort.<\/li>\n<li>And now, all the duplicate nodes will be adjacent, and we can now easily remove these duplicate nodes from the list by comparing the adjacent nodes by iterating the list.<\/li>\n<\/ul>\n<p>The time complexity for this approach will be O(nLogn), because sorting a list takes (nlogn) time.<\/p>\n<p>Let&#8217;s see if we can further reduce the time complexity in the next approach. Any Ideas?<\/p>\n<ul>\n<li>If not, its okay; we will thoroughly see how to reduce time complexity in the next section.<\/li>\n<\/ul>\n<p>Let&#8217;s move to the next approach.<\/p>\n<h3>Approach 3 (Using set)to remove duplicates from unsorted linked list<\/h3>\n<p>In this approach, we will make use to set to store the occurrences of the elements of the list.<\/p>\n<ul>\n<li>The most efficient way to remove the duplicate element is to use a set to store the occurrence of the elements present in the Linked List. <\/li>\n<li>Now, we traverse the Linked List and if the element in the current node is already present in the set:\n<ul>\n<li>We will remove the current node from the linked list. <\/li>\n<li>Else we store the element in the set and move forward in the linked list.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Let&#8217;s see the algorithm for this approach.<\/p>\n<h2>Algorithm to remove duplicates from unsorted linked list<\/h2>\n<ul>\n<li>Take a set <strong>seen<\/strong> to store the elements of the Linked List. Also remember that the set stores unique elements.<\/li>\n<li>Take a variable <strong>curr<\/strong> and initialize it with head of the linked list.<\/li>\n<li>Take a variable <strong>prev<\/strong> and initialize it with NULL.<\/li>\n<li>Traverse the linked list till <strong>curr<\/strong> do not become NULL.<\/li>\n<li>While traversing the list:\n<ul>\n<li>If the element in the current node <strong>curr<\/strong> is already present in the set then remove the current node from the linked list by assigning next of <strong>curr<\/strong> to the next of previous node of <strong>curr<\/strong> <strong>(prev-&gt;next = curr-&gt;next)<\/strong> and delete the node <strong>curr<\/strong>.<\/li>\n<li>Else, insert the element in the set and move <strong>prev<\/strong> forward by moving <strong>prev<\/strong> to the position of <strong>curr<\/strong>.<\/li>\n<li>Move <strong>curr<\/strong> forward by moving <strong>curr<\/strong> to the next of prev.<\/li>\n<\/ul>\n<\/li>\n<li>Finally, after the traversal, our resultant list will be free from duplicates.<\/li>\n<\/ul>\n<h3>Dry Run to remove duplicates from unsorted linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_1-8.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_2-8.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/09\/p_3-4.png\" alt=\"\" \/><\/p>\n<h2>Code Implementation to remove duplicates from unsorted linked list<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_5045 {\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_5045 .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_5045 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_5045 .wpsm_nav-tabs > li.active > a, #tab_container_5045 .wpsm_nav-tabs > li.active > a:hover, #tab_container_5045 .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_5045 .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_5045 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_5045 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_5045 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_5045 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_5045 .wpsm_nav-tabs > li > a:hover , #tab_container_5045 .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_5045 .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_5045 .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_5045 .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_5045 .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_5045 .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_5045 .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_5045 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5045 .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_5045 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_5045 .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_5045 .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_5045\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_5045\">\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_5045_1\" aria-controls=\"tabs_desc_5045_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_5045_2\" aria-controls=\"tabs_desc_5045_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_5045_3\" aria-controls=\"tabs_desc_5045_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_5045_4\" aria-controls=\"tabs_desc_5045_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_5045\">\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_5045_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\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/\/ Utility function to create a new Node\r\nstruct Node* newNode(int data)\r\n{\r\n    struct Node* temp =\r\n         (struct Node*)malloc(sizeof(struct 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 remove duplicates from a\r\n   unsorted linked list *\/\r\nvoid removeDuplicates(struct Node* start)\r\n{\r\n    struct Node *ptr1, *ptr2, *dup;\r\n    ptr1 = start;\r\n \r\n    \/* Pick elements one by one *\/\r\n    while (ptr1 != NULL &amp;&amp; ptr1-&gt;next != NULL) {\r\n        ptr2 = ptr1;\r\n \r\n        \/* Compare the picked element with rest\r\n           of the elements *\/\r\n        while (ptr2-&gt;next != NULL) {\r\n            \/* If duplicate then delete it *\/\r\n            if (ptr1-&gt;data == ptr2-&gt;next-&gt;data) {\r\n                \/* sequence of steps is important here *\/\r\n                dup = ptr2-&gt;next;\r\n                ptr2-&gt;next = ptr2-&gt;next-&gt;next;\r\n                free(dup);\r\n            }\r\n            else \/* This is tricky *\/\r\n                ptr2 = ptr2-&gt;next;\r\n        }\r\n        ptr1 = ptr1-&gt;next;\r\n    }\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        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 function *\/\r\nint main()\r\n{\r\n    \/* The constructed linked list is:\r\n     10-&gt;12-&gt;11-&gt;11-&gt;12-&gt;11-&gt;10*\/\r\n    struct Node* start = newNode(10);\r\n    start-&gt;next = newNode(12);\r\n    start-&gt;next-&gt;next = newNode(11);\r\n    start-&gt;next-&gt;next-&gt;next = newNode(11);\r\n    start-&gt;next-&gt;next-&gt;next-&gt;next = newNode(12);\r\n    start-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(11);\r\n    start-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(10);\r\n \r\n    printf(&quot;Linked list before removing duplicates &quot;);\r\n    printList(start);\r\n \r\n    removeDuplicates(start);\r\n \r\n    printf(&quot;&#92;nLinked list after removing duplicates &quot;);\r\n    printList(start);\r\n \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_5045_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\nstruct Node\r\n{\r\n    int data;\r\n    struct Node *next;\r\n};\r\n\r\n\/* Using this function we will be creating a new node *\/\r\nstruct Node *newNode(int data)\r\n{\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\/* Using this function we will be removing the duplicates elements from the given unsorted linked list *\/\r\nvoid removeDuplicatesFromList(struct Node *head)\r\n{\r\n    unordered_set&lt;int&gt; seen; \r\n    struct Node *curr = head;\r\n    struct Node *prev = NULL;\r\n    while (curr != NULL)\r\n    {\r\n        if (seen.find(curr-&gt;data) != seen.end())\r\n        {\r\n            prev-&gt;next = curr-&gt;next;\r\n            delete (curr); \r\n        }\r\n        else\r\n        {\r\n            seen.insert(curr-&gt;data); \r\n            prev = curr; \r\n        }\r\n        curr = prev-&gt;next; \r\n    }\r\n}\r\n\r\n\/* Using this function we will be printing the 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\nint main()\r\n{\r\n    struct Node *head = newNode(10);\r\n    head-&gt;next = newNode(12);\r\n    head-&gt;next-&gt;next = newNode(11);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(15);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = newNode(12);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(11);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next-&gt;next = newNode(10);\r\n    cout&lt;&lt;&quot;Linked list before removing duplicates&quot;&lt;&lt;endl;\r\n    printList(head);cout&lt;&lt;endl;\r\n    removeDuplicatesFromList(head);\r\n    cout&lt;&lt;&quot;Linked list after removing duplicates&quot;&lt;&lt;endl;\r\n    printList(head);cout&lt;&lt;endl;\r\n    return 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_5045_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\nimport java.util.HashSet;\r\n\r\nclass Unsorted \r\n{\r\n    static class node \r\n    {\r\n        int val;\r\n        node next;\r\n\r\n        public node(int val) \r\n        { \r\n            this.val = val; \r\n        }\r\n    }\r\n    \/* Function to remove duplicates from a\r\n    unsorted linked list *\/\r\n    static void removeDuplicate(node head)\r\n    {\r\n        \/\/ Hash to store seen values\r\n        HashSet&lt;integer&gt; hs = new HashSet&lt;&gt;();\r\n\r\n        \/* Pick elements one by one *\/\r\n        node current = head;\r\n        node prev = null;\r\n        while (current != null) {\r\n            int curval = current.val;\r\n\r\n            \/\/ If current value is seen before\r\n            if (hs.contains(curval)) {\r\n                prev.next = current.next;\r\n            }\r\n            else {\r\n                hs.add(curval);\r\n                prev = current;\r\n            }\r\n            current = current.next;\r\n        }\r\n    }\r\n    static void printList(node head)\r\n    {\r\n        while (head != null) {\r\n            System.out.print(head.val + &quot; &quot;);\r\n            head = head.next;\r\n        }\r\n    }\r\n    public static void main(String[] args)\r\n    {\r\n        node start = new node(10);\r\n        start.next = new node(12);\r\n        start.next.next = new node(11);\r\n        start.next.next.next = new node(11);\r\n        start.next.next.next.next = new node(12);\r\n        start.next.next.next.next.next = new node(11);\r\n        start.next.next.next.next.next.next = new node(10);\r\n\r\n        System.out.println(&quot;Linked list before removing duplicates :&quot;);\r\n        printList(start);\r\n        removeDuplicate(start);\r\n        System.out.println(&quot;&#92;nLinked list after removing duplicates :&quot;);\r\n        printList(start);\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_5045_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\tdef __init__(self, data):\r\n\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\n\r\nclass LinkedList:\r\n\r\n\tdef __init__(self):\r\n\r\n\t\tself.head = None\r\n\r\n\tdef printlist(self):\r\n\r\n\t\ttemp = self.head\r\n\r\n\t\twhile (temp):\r\n\t\t\tprint(temp.data, end=&quot; &quot;)\r\n\t\t\ttemp = temp.next\r\n\r\n\tdef removeDuplicates(self, head):\r\n\r\n\t\tif self.head is None or self.head.next is None:\r\n\t\t\treturn head\r\n\r\n\t\thash = set()\r\n\r\n\t\tcurrent = head\r\n\t\thash.add(self.head.data)\r\n\r\n\t\twhile current.next is not None:\r\n\r\n\t\t\tif current.next.data in hash:\r\n\t\t\t\tcurrent.next = current.next.next\r\n\t\t\telse:\r\n\t\t\t\thash.add(current.next.data)\r\n\t\t\t\tcurrent = current.next\r\n\r\n\t\treturn head\r\n\r\n\r\nif __name__ == &quot;__main__&quot;:\r\n\r\n\tllist = LinkedList()\r\n\tllist.head = Node(10)\r\n\tsecond = Node(12)\r\n\tthird = Node(11)\r\n\tfourth = Node(15)\r\n\tfifth = Node(12)\r\n\tsixth = Node(11)\r\n\tseventh = Node(10)\r\n\r\n\tllist.head.next = second\r\n\tsecond.next = third\r\n\tthird.next = fourth\r\n\tfourth.next = fifth\r\n\tfifth.next = sixth\r\n\tsixth.next = seventh\r\n\r\n\tprint(&quot;Linked List before removing Duplicates.&quot;)\r\n\tllist.printlist()\r\n\tllist.removeDuplicates(llist.head)\r\n\tprint(&quot;&#92;nLinked List after removing duplicates.&quot;)\r\n\tllist.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_5045 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_5045 a\"),jQuery(\"#tab-content_5045\"));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<p>**Output**<\/p>\n<p>    Linked list before removing duplicates<\/p>\n<p>    10 12 11 15 12 11 10<\/p>\n<p>    Linked list after removing duplicates<\/p>\n<p>    10 12 11 15<\/p>\n<p><strong>Time Complexity<\/strong>: O(n), where n is the number of nodes in the given linked list.<\/p>\n<p>**Conclusion:**<br \/>\nIn conclusion, removing duplicates from an unsorted linked list is a common problem in programming.  Through the utilization of efficient algorithms and data structures like hash sets or temporary buffers, we can proficiently detect and eliminate duplicate elements from the linked list. This involves traversing the linked list, comparing each element with the rest, and removing any duplicates encountered. By tackling this issue, we can guarantee the integrity and uniqueness of the data held within the linked list.<\/p>\n<p>## FAQs related to Removing Duplicates from an Unsorted Linked List<br \/>\nSome Frequently asked questions related to remove duplicates from unsorted linked list are:<br \/>\n**Q1: Can we use a sorting algorithm to remove duplicates from an unsorted linked list?**<br \/>\n**Ans.** Yes, it is possible to use sorting algorithms like to merge sort or quicksort to remove duplicates from an unsorted linked list. However, this approach has a higher time complexity compared to using hash sets or temporary buffers.<\/p>\n<p>**Q2: What is the time complexity of removing duplicates from an unsorted linked list?**<br \/>\n**Ans.** The time complexity depends on the approach used. If a hash set or temporary buffer is utilized, the time complexity is O(n), where n is the number of elements in the linked list. However, if sorting algorithms are employed, the time complexity can be O(n log n) or higher.<\/p>\n<p>**Q3: Can we modify the linked list in-place to remove duplicates?**<br \/>\n**Ans.** Yes, it is possible to modify the linked list in-place to remove duplicates. This involves iterating through the linked list and removing duplicates as they are encountered, without using additional data structures. This approach requires careful manipulation of pointers and can have a time complexity of O(n^2).<\/p>\n<p>**Q4: How can we handle the removal of duplicates if the linked list is very large and cannot fit in memory?**<br \/>\n**Ans.** If the linked list is extremely large and cannot fit in memory, external sorting algorithms or disk-based data structures can be employed to handle the removal of duplicates. These techniques involve dividing the linked list into smaller parts and processing them sequentially or by utilizing external storage.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although linked lists can store unique values, there are situations where duplicates can be present. Hence, the focus of this article is to explain how to remove duplicates from unsorted linked lists. How to Remove Duplicates from Unsorted Linked List In this problem, we are given an unsorted LinkedList and are asked to remove the [&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-5043","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>How to remove duplicates from an unsorted Linked List<\/title>\n<meta name=\"description\" content=\"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.\" \/>\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-an-unsorted-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to remove duplicates from an unsorted Linked List\" \/>\n<meta property=\"og:description\" content=\"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-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:19:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-27T12:20:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.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=\"7 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-an-unsorted-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Remove duplicates from an unsorted linked list\",\"datePublished\":\"2021-09-18T11:19:17+00:00\",\"dateModified\":\"2023-07-27T12:20:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/\"},\"wordCount\":1292,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/\",\"name\":\"How to remove duplicates from an unsorted Linked List\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png\",\"datePublished\":\"2021-09-18T11:19:17+00:00\",\"dateModified\":\"2023-07-27T12:20:23+00:00\",\"description\":\"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-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 an unsorted 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":"How to remove duplicates from an unsorted Linked List","description":"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.","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-an-unsorted-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"How to remove duplicates from an unsorted Linked List","og_description":"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.","og_url":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-09-18T11:19:17+00:00","article_modified_time":"2023-07-27T12:20:23+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Remove duplicates from an unsorted linked list","datePublished":"2021-09-18T11:19:17+00:00","dateModified":"2023-07-27T12:20:23+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/"},"wordCount":1292,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/","name":"How to remove duplicates from an unsorted Linked List","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png","datePublished":"2021-09-18T11:19:17+00:00","dateModified":"2023-07-27T12:20:23+00:00","description":"Learn how to remove duplicates from an unsorted Linked List in the most efficient way possible.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645001319252-Article_149.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/remove-duplicates-from-an-unsorted-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 an unsorted 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\/5043","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=5043"}],"version-history":[{"count":12,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5043\/revisions"}],"predecessor-version":[{"id":17377,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/5043\/revisions\/17377"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=5043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=5043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=5043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}