Python solution


  • 0
    W
    class Solution(object):
    
        def deleteDuplicates(self, head):
            d = {}
            node = head
            while node:
                d[node.val] = d.get(node.val, 0) + 1
                node = node.next
            
            duplicates = {k for k, v in d.items() if v > 1}
            
            dummy = ListNode(None)
            dummy.next = head
            
            node = dummy
            while node:
                if node.next and node.next.val in duplicates:
                    node.next = node.next.next
                else:
                    node = node.next
            
            return dummy.next
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.