Hone logo
Hone
Problems

Remove Duplicates from Sorted List II

This problem challenges you to modify a sorted linked list by removing all occurrences of consecutive duplicate values. The goal is to create a new linked list containing only unique elements, preserving the original order while eliminating any sequences of identical values. This is a common problem in data cleaning and optimization, ensuring data integrity and efficiency.

Problem Description

Given the head of a sorted linked list, remove all consecutive duplicate nodes. A "consecutive duplicate" is defined as a node whose value is the same as the value of the next node. You should modify the linked list in-place, meaning you should not create a new list. The function should return the head of the modified linked list.

What needs to be achieved:

  • Iterate through the sorted linked list.
  • Identify consecutive duplicate nodes.
  • Remove all consecutive duplicate nodes while preserving the order of the remaining unique nodes.
  • Return the head of the modified linked list.

Key Requirements:

  • The linked list is sorted in ascending order.
  • The modification must be done in-place.
  • The original order of the unique elements must be maintained.

Expected Behavior:

The function should correctly remove all consecutive duplicates from the linked list. If the list is empty or contains no duplicates, the original list should be returned unchanged.

Edge Cases to Consider:

  • Empty list: The function should return null.
  • List with only one element: The function should return the head of the list.
  • List with all duplicate elements: The function should return null.
  • Consecutive duplicates at the beginning of the list.
  • Consecutive duplicates at the end of the list.
  • Alternating unique and duplicate sequences.

Examples

Example 1:

Input: [1,2,2,3,4,4,4,5]
Output: [1,2,3,4,5]
Explanation: The original list contains consecutive duplicates (2, 2), (4, 4, 4).  The output list removes these duplicates, resulting in [1, 2, 3, 4, 5].

Example 2:

Input: [1,1,1,2,3]
Output: [1,2,3]
Explanation: The original list contains consecutive duplicates (1, 1, 1). The output list removes these duplicates, resulting in [1, 2, 3].

Example 3:

Input: []
Output: null
Explanation: An empty list should return null.

Example 4:

Input: [1]
Output: [1]
Explanation: A list with a single element should return the same list.

Constraints

  • The number of nodes in the linked list can range from 0 to 1000.
  • The values of the nodes are integers.
  • The linked list is sorted in ascending order.
  • The solution should have a time complexity of O(n), where n is the number of nodes in the linked list.
  • The solution should have a space complexity of O(1) (in-place modification).

Notes

Consider using a pointer to traverse the list and another pointer to keep track of the previous node. When you encounter a duplicate, skip over all consecutive duplicates by advancing the pointer until a unique element is found. Remember to update the next pointer of the previous node to point to the unique element. Think about how to handle the case where the head of the list needs to be changed.

Loading editor...
plaintext