---
title: '82. Remove Duplicates from Sorted List II'
description: You are given the head of a sorted linked list
icon: dot
topics:
  - { name: "Linked List", slug: "linked-list" }
  - { name: "Two Pointers", slug: "two-pointers" }
issue: "https://github.com/prdlk/leetcode/issues/84"
companies:
  - { name: "Amazon", period: "Three Months", frequency: 34.6 }
  - { name: "Google", period: "Three Months", frequency: 23.9 }
  - { name: "Bloomberg", period: "Six Months", frequency: 34.2 }
  - { name: "Meta", period: "Six Months", frequency: 24.5 }
sidebar:
  label: 'Remove Duplicates from Sorted List II'
  badge: 'Medium'
---

### 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.

## Solution

```py
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: ListNode | None) -> ListNode | None:
        sentinel = ListNode(0, head)
        pred = sentinel

        while head:
            if head.next and head.val == head.next.val:
                while head.next and head.val == head.next.val:
                    head = head.next

                pred.next = head.next
            else:
                pred = pred.next

            head = head.next

        return sentinel.next
```
