All prompts are owned by LeetCode. To view the prompt, click the title link above.
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.
# 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