Remove duplicates from sorted DLL

Pattern: Doubly and circular lists

medium

In a sorted list copies are neighbours. Stand on a node and unlink its next while the next has the same value, fixing the prev link of the node that slides in.

The problem

Given the head of a sorted doubly linked list, remove the extra copies of every value so each appears once, and return the head. For example, 1 ⇄ 1 ⇄ 1 ⇄ 2 ⇄ 3 ⇄ 4 becomes 1 ⇄ 2 ⇄ 3 ⇄ 4.

The function name removeDuplicates matches the takeUforward practice page. Use the link above for the exact statement and limits. In this guide's code the node type is DNode.

Think first

  • Where are the copies of a value in a sorted list?
  • After unlinking cur.next, which prev pointer must change?
  • Should cur move after unlinking?

Hints

    Brute force, and why it fails

    A set of seen values works on unsorted lists too, with O(n) memory. Sorted order makes neighbours enough, so O(1) memory.

    The key insight

    The same neighbour check as for a singly linked list, plus one prev fix per removed node.

    Dry run

    10cur
    11 
    12 
    23 
    34 
    45 

    The list is 1 ⇄ 1 ⇄ 1 ⇄ 2 ⇄ 3 ⇄ 4. cur starts on the first 1.

    Step 1 of 4

    The plan

    cur = head
    while cur and cur.next:
        if cur.next.val == cur.val:
            dup = cur.next
            cur.next = dup.next
            if dup.next: dup.next.prev = cur
        else: cur = cur.next
    return head
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Each step either removes a node or moves forward.

    Edge cases

    • All equal: one node.
    • No copies: unchanged.
    • Copies at the end: the last kept node's next becomes null.
    I've built my logic, show the code

    Java

    class Solution {
        public DNode removeDuplicates(DNode head) {
            DNode cur = head;
            while (cur != null && cur.next != null) {
                if (cur.next.val == cur.val) {      // a copy: unlink cur.next
                    DNode dup = cur.next;
                    cur.next = dup.next;
                    if (dup.next != null) dup.next.prev = cur;
                } else {
                    cur = cur.next;
                }
            }
            return head;
        }
    }

    C++

    class Solution {
    public:
        DNode* removeDuplicates(DNode* head) {
            DNode* cur = head;
            while (cur != NULL && cur->next != NULL) {
                if (cur->next->val == cur->val) {   // a copy: unlink cur->next
                    DNode* dup = cur->next;
                    cur->next = dup->next;
                    if (dup->next != NULL) dup->next->prev = cur;
                } else {
                    cur = cur->next;
                }
            }
            return head;
        }
    };

    Python

    from lib import DNode
    
    class Solution:
        def removeDuplicates(self, head: DNode) -> DNode:
            cur = head
            while cur is not None and cur.next is not None:
                if cur.next.val == cur.val:         # a copy: unlink cur.next
                    dup = cur.next
                    cur.next = dup.next
                    if dup.next is not None:
                        dup.next.prev = cur
                else:
                    cur = cur.next
            return head

    Common mistakes

    • Forgetting dup.next.prev = cur.
    • Moving cur after an unlink, which leaves a third copy.
    • Reading dup.next.prev when dup.next is null.

    Similar problems