82. Remove Duplicates from Sorted List II
Question:
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:
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
Input: head = [1,1,1,2,3]
Output: [2,3]
Source code
Version 1
Idea:
At the first while loop, it ensures no nullptr
in next two pointers. Then, check the current value whether it is the same as the next value. If it is duplicate, store this value and compare with the next value until different value.
Time complexity: O(n)
Space complexity: O(1)
1 | /** |