Python Solution using Dictionary


  • 0
    M
    class Solution(object):
        def delete(self, node):
            node.val = node.next.val
            node.next = node.next.next
        
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            dups = {}
            curr = head
            while curr:
                dups[curr.val] = dups.get(curr.val, 0) + 1
                curr = curr.next
            d = ListNode(0)
            d.next = head
            prev = d
            curr = head
            while curr:
                if dups[curr.val] > 1:
                    if not curr.next:
                        prev.next = None
                        break
                    self.delete(curr)
                else:
                    curr = curr.next
                    prev = prev.next
            return d.next
            
    

Log in to reply
 

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