Advertisement

Remove Duplicates from Sorted List - LeetCode 83 Solution

Remove Duplicates from Sorted List - Complete Solution Guide

Remove Duplicates from Sorted List is LeetCode problem 83, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the head of a sorted linked list, delete all duplicates such that each element appears only once . Return the linked list sorted as well . Example 1: Input: head = [1,1,2] Output: [1,2] Example 2: Input: head = [1,1,2,3,3] Output: [1,2,3] Constraints: The number of nodes in the list is in the range [0, 300] . -100 <= Node.val <= 100 The list is guaranteed to be sorted in ascending order.

Detailed Explanation

You're presented with a linked list, but with a crucial guarantee: it's already perfectly sorted in ascending order. Your task is to modify this list in-place so that every value appears only once. For instance, if you're handed `[1,1,2]`, your output should be `[1,2]`. Similarly, an input like `[1,1,2,3,3]` needs to become `[1,2,3]`. The final list, of course, must remain sorted.

Solution Approach

The provided solution leverages the 'sorted list' property to its fullest. It employs a single `current` pointer that traverses the list. The core idea is simple: because all duplicates will appear consecutively, we only need to compare `current.val` with `current.next.val`. If they are identical, we've found a duplicate (`current.next` is the duplicate). Instead of advancing `current` to this duplicate, we effectively 'delete' it by making `current.next` point to `current.next.next`, thereby bypassing the duplicate node. `current` remains in its position, ready to check if the *new* `current.next` is also a duplicate of `current.val` (which handles cases like `1,1,1`). If `current.val` and `current.next.val` are different, it means `current.val` is unique at this point, so we safely advance `current` to `current.next` and continue our scan. This process repeats until `current` or `current.next` becomes `None`, indicating the end of the list or no more pairs to compare.

Step-by-Step Algorithm

  1. Step 1: Initialize a pointer `current` to the head of the linked list.
  2. Step 2: Iterate through the list using a `while` loop, continuing as long as `current` is not `null` and `current.next` is not `null`.
  3. Step 3: If `current.val` is equal to `current.next.val`, remove the duplicate node by updating `current.next` to point to `current.next.next`. This effectively skips the duplicate node.
  4. Step 4: If `current.val` is not equal to `current.next.val`, move to the next node by setting `current = current.next`.
  5. Step 5: Return the modified `head` of the linked list.

Key Insights

  • **Leveraging Sortedness for Adjacency Checks**: The fundamental insight is that because the list is sorted, any duplicates will always be directly next to each other. This eliminates the need for auxiliary data structures like hash sets or complex look-ahead logic; a simple `current.val == current.next.val` comparison is sufficient.
  • **In-place Pointer Rewiring**: The solution cleverly re-wires `next` pointers to 'skip' duplicate nodes. When `current.val` matches `current.next.val`, assigning `current.next = current.next.next` effectively removes `current.next` from the list's chain without allocating new memory or shifting elements. This operation also naturally handles consecutive duplicates (e.g., `1,1,1`) by allowing `current` to stay put and re-evaluate its *new* next node against its own value.
  • **Single Pass, Constant Space Efficiency**: The algorithm traverses the linked list exactly once, examining each node a constant number of times. This results in an optimal time complexity of O(N), where N is the number of nodes. Furthermore, since it only uses a single pointer (`current`) and modifies the list directly, its space complexity is O(1), making it highly efficient for memory-constrained environments.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Bloomberg, Microsoft, Nvidia, Oracle, Revolut.