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:
[0, 300]
.-100 <= Node.val <= 100
The core challenge of this problem is to remove all nodes that have duplicate values from a sorted linked list, ensuring that only distinct values remain. This is significant in scenarios where data integrity and uniqueness are crucial, such as in databases or data processing pipelines.
Common pitfalls include not handling edge cases like an empty list or a list where all elements are duplicates.
To solve this problem, we can use a two-pointer technique to traverse the linked list and identify duplicates. Here's a step-by-step approach:
A naive solution would involve using a nested loop to compare each node with every other node, but this would result in a time complexity of O(n^2), which is not efficient for larger lists.
An optimized solution involves using a single pass through the list with a dummy node to handle edge cases more gracefully. This approach ensures a time complexity of O(n).
Here's a step-by-step breakdown of the optimized algorithm:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var deleteDuplicates = function(head) {
// Create a dummy node to handle edge cases
let dummy = new ListNode(0);
dummy.next = head;
// Initialize pointers
let prev = dummy;
let current = head;
while (current !== null) {
// Check for duplicates
if (current.next !== null && current.val === current.next.val) {
// Skip all nodes with the same value
while (current.next !== null && current.val === current.next.val) {
current = current.next;
}
// Link prev to the node after the last duplicate
prev.next = current.next;
} else {
// Move prev to current
prev = prev.next;
}
// Move current to the next node
current = current.next;
}
// Return the modified list
return dummy.next;
};
The time complexity of this approach is O(n) because we traverse the list only once. The space complexity is O(1) as we are using a constant amount of extra space.
Potential edge cases include:
These cases are handled by the dummy node and the traversal logic.
To test the solution comprehensively, consider the following test cases:
head = []
head = [1,1,1]
head = [1,2,3]
head = [1,2,2,3,4,4,5]
Use a testing framework like Jest or Mocha to automate these tests.
When approaching such problems, consider the following tips:
Practice similar problems to improve your problem-solving skills.
In this blog post, we discussed how to remove duplicates from a sorted linked list using an optimized approach with O(n) time complexity. Understanding and solving such problems is crucial for improving your algorithmic thinking and coding skills.
Keep practicing and exploring further to master these concepts.