{"id":687,"date":"2025-07-18T14:34:35","date_gmt":"2025-07-18T09:04:35","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=687"},"modified":"2025-07-18T14:34:37","modified_gmt":"2025-07-18T09:04:37","slug":"remove-duplicates-from-a-sorted-doubly-linked-list","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/","title":{"rendered":"Remove duplicates from a sorted doubly linked list | GFG Practice | Optimal Solution"},"content":{"rendered":"\n<p>Given the head of a sorted doubly linked list,\u00a0remove all duplicate nodes from the list. Each node should appear\u00a0only once in the final list.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/remove-duplicates-from-a-sorted-doubly-linked-list\/1\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<br><\/strong>n = 6<strong><br><\/strong>1&lt;->1&lt;->1&lt;->2&lt;->3&lt;->4<br><strong>Output:<br><\/strong>1&lt;->2&lt;->3&lt;->4<br><strong>Explanation:<\/strong><br>Only the first occurance of node with value 1 is <br>retained, rest nodes with value = 1 are deleted.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:\n<\/strong>n = 7<strong>\n<\/strong>1&lt;-&gt;2&lt;-&gt;2&lt;-&gt;3&lt;-&gt;3&lt;-&gt;4&lt;-&gt;4\n<strong>Output:\n<\/strong>1&lt;-&gt;2&lt;-&gt;3&lt;-&gt;4\n<strong>Explanation:<\/strong>\nOnly the first occurance of nodes with values 2,3 and 4 are \nretained, rest repeating nodes are deleted.<\/pre>\n\n\n\n<p><strong>Your Task:<\/strong><br>You have to complete the method&nbsp;<strong>removeDuplicates<\/strong>() which takes&nbsp;<strong>1<\/strong>&nbsp;argument: the&nbsp;<strong>head<\/strong>&nbsp;of the linked list. &nbsp;Your function should&nbsp;return a pointer to a linked list with no duplicate&nbsp;element.<\/p>\n\n\n\n<p><strong>Constraint:<\/strong><br>1 &lt;= n &lt;= 10<sup>5<\/sup><br><strong>Expected Time Complexity:&nbsp;<\/strong>O(n)<br><strong>Expected Space Complexity:&nbsp;<\/strong>O(1)<\/p>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-ac5ca50c-7bd7-4403-889a-a2e8bd4fb69b\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#0-solution-optimal-approach-single-pass-traversal\" style=\"\">Solution: Optimal Approach (Single Pass Traversal)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#8-alternative-approach-more-common-implementation\" style=\"\">Alternative Approach (More Common Implementation)<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/remove-duplicates-from-a-sorted-doubly-linked-list\/#9-summary\" style=\"\">Summary<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-optimal-approach-single-pass-traversal\">Solution: Optimal Approach (Single Pass Traversal)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like organizing a bookshelf where books are arranged alphabetically, but you have multiple copies of some books! Since the books are already sorted, all duplicate copies will be sitting right next to each other. You can simply walk through the shelf and whenever you see two identical books next to each other, remove one of them. In a doubly linked list, we do the same &#8211; traverse the list and remove duplicate nodes that are adjacent to each other.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Start Traversal<\/strong>: Begin from the head and traverse through each node<\/li>\n\n\n\n<li><strong>Check for Duplicates<\/strong>: For each node, check if its data matches the previous node&#8217;s data<\/li>\n\n\n\n<li><strong>Remove Duplicate<\/strong>: When a duplicate is found, remove the current node by updating the links<\/li>\n\n\n\n<li><strong>Update Links<\/strong>: Properly connect the previous and next nodes to skip the duplicate<\/li>\n\n\n\n<li><strong>Handle Head Update<\/strong>: If the head node itself is a duplicate, update the head pointer<\/li>\n\n\n\n<li><strong>Continue<\/strong>: Move to the next node and repeat until the end<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    #Function to remove duplicates from sorted doubly linked list.\n    def removeDuplicates(self, head):\n        cur = head  # Current node pointer for traversal\n        \n        while cur:\n            # Check if current node is duplicate of previous node\n            if cur.prev and cur.prev.data == cur.data:\n                # Handle case where previous node is the head\n                if cur.prev == head:\n                    cur.prev = None        # Remove backward link\n                    head = cur            # Update head to current node\n                else:\n                    # Remove the previous duplicate node by updating links\n                    cur.prev.prev.next = cur     # Connect prev's prev to current\n                    cur.prev = cur.prev.prev     # Connect current to prev's prev\n            \n            cur = cur.next  # Move to next node\n        \n        return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #6A9955\">#Function to remove duplicates from sorted doubly linked list.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeDuplicates<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        cur = head  <\/span><span style=\"color: #6A9955\"># Current node pointer for traversal<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> cur:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Check if current node is duplicate of previous node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cur.prev <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> cur.prev.data == cur.data:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #6A9955\"># Handle case where previous node is the head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cur.prev == head:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    cur.prev = <\/span><span style=\"color: #569CD6\">None<\/span><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Remove backward link<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    head = cur            <\/span><span style=\"color: #6A9955\"># Update head to current node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    <\/span><span style=\"color: #6A9955\"># Remove the previous duplicate node by updating links<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    cur.prev.prev.next = cur     <\/span><span style=\"color: #6A9955\"># Connect prev&#39;s prev to current<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    cur.prev = cur.prev.prev     <\/span><span style=\"color: #6A9955\"># Connect current to prev&#39;s prev<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            cur = cur.next  <\/span><span style=\"color: #6A9955\"># Move to next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>This solution traverses the doubly linked list and removes duplicate nodes by updating the forward and backward links. When a duplicate is detected (current node&#8217;s data equals previous node&#8217;s data), it removes the previous node by reconnecting the links. The algorithm handles the special case where the head node itself is part of a duplicate pair. However, there&#8217;s an important note: this implementation removes the previous node when a duplicate is found, rather than the more typical approach of removing the current node.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>1 &lt;-&gt; 1 &lt;-&gt; 2 &lt;-&gt; 3 &lt;-&gt; 3<\/code><\/p>\n\n\n\n<p><strong>Initial Setup:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur = 1 (first node), head = 1<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 1:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur.prev = None, so no duplicate check<\/li>\n\n\n\n<li>cur = 1 (second node)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 2:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur.prev.data = 1, cur.data = 1 (duplicate found!)<\/li>\n\n\n\n<li>cur.prev == head, so update head = cur (second 1)<\/li>\n\n\n\n<li>cur.prev = None<\/li>\n\n\n\n<li>cur = 2<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 3:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur.prev.data = 1, cur.data = 2 (no duplicate)<\/li>\n\n\n\n<li>cur = 3 (first occurrence)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 4:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur.prev.data = 2, cur.data = 3 (no duplicate)<\/li>\n\n\n\n<li>cur = 3 (second occurrence)<\/li>\n<\/ul>\n\n\n\n<p><strong>Step 5:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>cur.prev.data = 3, cur.data = 3 (duplicate found!)<\/li>\n\n\n\n<li>cur.prev != head, so update links<\/li>\n\n\n\n<li>Connect 2 directly to second 3, remove first 3<\/li>\n\n\n\n<li>cur = None<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>1 &lt;-&gt; 2 &lt;-&gt; 3<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(n) &#8211; We traverse the list once, visiting each node exactly once<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(1) &#8211; We only use a few pointer variables, no extra space needed<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h3>\n\n\n\n<p>This approach is like walking through a line of people and whenever you find two people with the same name standing next to each other, you ask the first person to leave. Since everyone is already arranged in alphabetical order by name, all people with the same name will be standing together, making it easy to spot and remove duplicates.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-alternative-approach-more-common-implementation\">Alternative Approach (More Common Implementation)<\/h2>\n\n\n\n<p>While the provided solution works, a more intuitive approach would be to remove the current node when a duplicate is found:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Alternative approach (more common)\ndef removeDuplicates(self, head):\n    cur = head\n    while cur and cur.next:\n        if cur.data == cur.next.data:\n            # Remove cur.next node\n            duplicate = cur.next\n            cur.next = duplicate.next\n            if duplicate.next:\n                duplicate.next.prev = cur\n        else:\n            cur = cur.next\n    return head\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A9955\"># Alternative approach (more common)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">removeDuplicates<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">head<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    cur = head<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\"> cur <\/span><span style=\"color: #569CD6\">and<\/span><span style=\"color: #D4D4D4\"> cur.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> cur.data == cur.next.data:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #6A9955\"># Remove cur.next node<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            duplicate = cur.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            cur.next = duplicate.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> duplicate.next:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                duplicate.next.prev = cur<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            cur = cur.next<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> head<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<p>This approach is often more intuitive because it removes the &#8220;next&#8221; duplicate rather than the &#8220;previous&#8221; one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-summary\">Summary<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Approach<\/th><th>Time<\/th><th>Space<\/th><th>Difficulty<\/th><th>Best For<\/th><\/tr><\/thead><tbody><tr><td>Single Pass Traversal<\/td><td>O(n)<\/td><td>O(1)<\/td><td>Medium<\/td><td>Production code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>This&nbsp;<strong>optimal solution<\/strong>&nbsp;efficiently removes all duplicates in a single pass through the sorted doubly linked list. The key insight is that in a sorted list, all duplicate elements will be adjacent, so we only need to check neighboring nodes.<\/p>\n\n\n\n<p>The main challenge in doubly linked list problems is properly managing both forward and backward pointers when updating links. This problem demonstrates important concepts like link manipulation and edge case handling (such as updating the head pointer when necessary).<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given the head of a sorted doubly linked list,\u00a0remove all duplicate nodes from the list. Each node should appear\u00a0only once in the final list. Here&#8217;s the [Problem Link] to begin with. Example 1: Input:n = 61&lt;->1&lt;->1&lt;->2&lt;->3&lt;->4Output:1&lt;->2&lt;->3&lt;->4Explanation:Only the first occurance of node with value 1 is retained, rest nodes with value = 1 are deleted. Example<\/p>\n","protected":false},"author":1,"featured_media":688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[30,19],"class_list":{"0":"post-687","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-doubly-linked-list","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/remove-duplicates-from-a-sorted-doubly-linked-list-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=687"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/687\/revisions"}],"predecessor-version":[{"id":689,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/687\/revisions\/689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}