Python solution with dictionary, easy to understand.


  • 0
    C
    def deleteDuplicates(self, head):
        dic = {}
        node = head
        while node:
            dic[node.val] = dic.get(node.val, 0) + 1
            node = node.next
        node = head
        dummy = pre = ListNode(0)
        dummy.next = head
        while node:
            if dic[node.val] > 1:
                tmp = node
                for _ in xrange(dic[node.val]):
                    tmp = tmp.next
                pre.next = tmp
                node = tmp
            else:
                pre = node
                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.