> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove Duplicates from Sorted List II

> Tested Python solution for LeetCode 82 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 82, [Medium](/catalog/medium). Topics: [Linked List](/catalog/topics/linked-list), [Two Pointers](/catalog/topics/two-pointers). [View on LeetCode](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/).

Generate this problem as a practice environment: tested reference solution, 23 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 82   # by problem number
lcpy gen -s remove_duplicates_from_sorted_list_ii   # by problem name
```

## Problem

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.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg)

```
Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]
```

![Example 2](https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg)

```
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

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_duplicates_from_sorted_list_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_duplicates_from_sorted_list_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import ListNode


class Solution:
    # Time: O(n)
    # Space: O(1)
    def delete_duplicates(self, head: ListNode[int] | None) -> ListNode[int] | None:
        dummy: ListNode[int] = ListNode(0)
        dummy.next = head
        prev = dummy

        while head is not None:
            if head.next is not None and head.val == head.next.val:
                dup = head.val
                while head is not None and head.val == dup:
                    head = head.next
                prev.next = head
            else:
                prev = head
                head = head.next

        return dummy.next
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.