Skip to content

Latest commit

 

History

History
55 lines (40 loc) · 1.36 KB

_82. Remove Duplicates from Sorted List II.md

File metadata and controls

55 lines (40 loc) · 1.36 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 04, 2024

Last updated : July 04, 2024


Related Topics : Linked List, Two Pointers

Acceptance Rate : 48.94 %


This question is extremly similar to Question 1836 and I was able to reuse my code for that instance.


Solutions

Python

# 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]:
        cnt = defaultdict(int)
        curr = head
        while curr :
            cnt[curr.val] += 1
            curr = curr.next

        dummy = ListNode()
        dummy.next = head
        dummyHolder = dummy

        while dummy and dummy.next :
            while dummy.next and cnt[dummy.next.val] > 1 :
                dummy.next = dummy.next.next
            dummy = dummy.next
        
        return dummyHolder.next