Remove Duplicates in a sorted Linked List

Pattern: Traversal and dummy nodes

easy

In a sorted list, copies sit next to each other. Stand on a node and keep skipping its next while that next has the same value. Move on only when the value changes.

No verified practice link yet. The guide below states the problem in full, so you can still solve it here.

The problem

Given the head of a linked list sorted in increasing order, delete the extra copies of every value so each appears once, and return the head. For example, 2 → 2 → 4 → 5 becomes 2 → 4 → 5, and 2 → 2 → 2 → 2 → 2 becomes 2.

This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet (LeetCode's Remove Duplicates from Sorted List is the same task). The function name removeDuplicates is this guide's own.

Think first

  • In a sorted list, where are the copies of a value?
  • When you skip one copy, should you move forward?
  • Can the head ever be deleted?

Hints

    Brute force, and why it fails

    Use a set of seen values (as for an unsorted list): O(n) time but O(n) memory. Sorting means neighbours are enough, so O(1) memory.

    The key insight

    Sorting groups equal values together. Then "is this a duplicate" only needs a look at the previous kept node.

    Dry run

    20cur
    21 
    42 
    53 

    The list is 2 → 2 → 4 → 5. cur starts at the head.

    Step 1 of 4

    The plan

    cur = head
    while cur is not null and cur.next is not null:
        if cur.next.val == cur.val: cur.next = cur.next.next
        else: cur = cur.next
    return head
    

    Complexity

    Time
    O(n)
    Space
    O(1)

    Every step either skips a node or moves forward.

    Edge cases

    • All equal: one node left.
    • No duplicates: unchanged.
    • Empty or one node: unchanged.
    I've built my logic, show the code

    Java

    class Solution {
        public ListNode removeDuplicates(ListNode head) {
            ListNode cur = head;
            while (cur != null && cur.next != null) {
                if (cur.next.val == cur.val) cur.next = cur.next.next;   // a copy: skip it, stay on cur
                else cur = cur.next;                                     // a new value: move on
            }
            return head;
        }
    }

    C++

    class Solution {
    public:
        ListNode* removeDuplicates(ListNode* head) {
            ListNode* cur = head;
            while (cur != NULL && cur->next != NULL) {
                if (cur->next->val == cur->val) cur->next = cur->next->next;   // a copy: skip it, stay on cur
                else cur = cur->next;                                          // a new value: move on
            }
            return head;
        }
    };

    Python

    from lib import ListNode
    
    class Solution:
        def removeDuplicates(self, head: ListNode) -> ListNode:
            cur = head
            while cur is not None and cur.next is not None:
                if cur.next.val == cur.val:
                    cur.next = cur.next.next        # a copy: skip it, stay on cur
                else:
                    cur = cur.next                  # a new value: move on
            return head

    Common mistakes

    • Moving cur after skipping, which misses a third copy.
    • Using this on an unsorted list, where copies may be far apart.
    • Checking cur.next.val without checking cur.next first.

    Similar problems