Quite ugly python soltion


  • 0
    O
    def deleteDuplicates(self, head):
        cur, newhead = head, None
        temp = newhead
        while cur:
            curnext = cur.next
            
            while curnext and curnext.val == cur.val:
                curnext = curnext.next            
            if not newhead or newhead==cur:
                newhead = cur if cur.next == curnext else curnext          <--assign new head                    
                temp = newhead    
            else:
                temp.next = cur if cur.next == curnext and  cur!=temp else curnext
               
                if temp.next and temp.next.next and temp.next.val != temp.next.next.val:
                    temp = temp.next                                 <-- append node to unique list
        
            cur = curnext
        return newhead
    

    Creating a new head and appending a node are the two main events. Kind of hacky but accepted solution


Log in to reply
 

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