Talentd
FleetCode

82. Remove Duplicates from Sorted List II

Medium52.8% acceptance11 companies

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.
5:22
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, val=0, next=None):
4# self.val = val
5# self.next = next
6class Solution:
7 def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
8
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3,3,4,4,5]
Input
[1,1,1,2,3]
Your code is saved on this device.Solution