Advertisement

Remove Duplicates from Sorted List II - LeetCode 82 Solution

Remove Duplicates from Sorted List II - Complete Solution Guide

Remove Duplicates from Sorted List II is LeetCode problem 82, a Medium 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 nodes that have duplicate numbers, leaving only distinct numbers from the original list . Return the linked list sorted as well . Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [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

The problem asks us to remove all nodes from a sorted linked list that have duplicate numbers, leaving only distinct numbers from the original list. The input is the head of a sorted linked list, and the output is the head of the modified list with duplicates removed. For example, if the input is [1,2,3,3,4,4,5], the output should be [1,2,5]. The linked list is guaranteed to be sorted in ascending order, and node values are within the range of -100 to 100. We must preserve the sorted order in the output list.

Solution Approach

The solution uses a dummy node to simplify the removal of nodes from the beginning of the list. It iterates through the linked list, checking for consecutive duplicate values. If a duplicate is found, it skips all nodes with that value. If a value is not a duplicate, it advances the 'pred' pointer. The `pred` pointer will point to the node just *before* the first duplicate in a sequence. When a sequence of duplicates is found, we update the `pred->next` pointer to `head`, effectively skipping the duplicates. If no duplicates are found, we move both `head` and `pred` forward.

Step-by-Step Algorithm

  1. Step 1: Create a dummy node and set its 'next' pointer to the head of the original list.
  2. Step 2: Initialize a 'pred' pointer to the dummy node. This pointer will track the node preceding the current 'head' node.
  3. Step 3: Iterate through the linked list using the 'head' pointer.
  4. Step 4: Inside the loop, check if the current node 'head' has a next node and if their values are equal. If they are, we have found a duplicate.
  5. Step 5: If a duplicate is found, store the duplicate value in a variable 'val_to_skip'.
  6. Step 6: Continue moving the 'head' pointer forward until it reaches a node with a different value or the end of the list, skipping all nodes with the duplicate value.
  7. Step 7: Update the 'next' pointer of the 'pred' node to point to the current 'head' node, effectively removing the duplicate nodes from the list.
  8. Step 8: If no duplicate is found (i.e., head.next is null or head.val != head.next.val), move both the 'pred' and 'head' pointers to the next node in the list.
  9. Step 9: Repeat steps 4-8 until the 'head' pointer reaches the end of the list.
  10. Step 10: Return the 'next' pointer of the dummy node, which is the new head of the modified linked list.

Key Insights

  • Insight 1: Because the list is sorted, duplicates will always appear consecutively. This allows us to easily identify and skip over them.
  • Insight 2: Using a dummy node simplifies the handling of the head of the list, especially if the head needs to be removed during the duplicate removal process.
  • Insight 3: We need two pointers to keep track of the previous node (pred) and the current node (head). This allows us to modify the 'next' pointer of the previous node to skip the duplicate nodes.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Linked List, Two Pointers.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, Google, Microsoft, Tencent.