LeetCode-Problems / 0082. Remove Duplicates from Sorted List II / 82. Remove Duplicates from Sorted List II.py
82. Remove Duplicates from Sorted List II.py
Raw
# 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: Optional[ListNode]) -> Optional[ListNode]:
        head_start = ListNode(0, head)
        prev_node = head_start

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

        return head_start.next