Python one pass solution and Python solution with dictionary.


  • 1
    C
    def deleteDuplicates1(self, head):
        node = head
        while node and node.next:
            if node.val == node.next.val:
                node.next = node.next.next
            else:
                node = node.next
        return head
        
    def deleteDuplicates(self, head):
        dic = {}
        node = head
        while node:
            dic[node.val] = dic.get(node.val, 0) + 1
            node = node.next
        node = head
        while node:
            tmp = node
            for _ in xrange(dic[node.val]):
                tmp = tmp.next
            node.next = tmp
            node = node.next
        return head

  • 0
    S

    the dic solution is nice for if the values weren't sorted in the linked list


Log in to reply
 

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