Simple Accepted Python Solution


  • 0
    S
    class Solution:
    
        def deleteDuplicates(self, head):
            virtual = ListNode(-1111)   # some dummy vaue as the header
            virtual.next = head
            dummy_head = virtual  # dummy head, always point to the header
            flag = False
    
            while virtual is not None:
                if virtual.next is None:
                    return dummy_head.next
              
                cur = virtual.next
    
                while cur.next is not None and cur.val == cur.next.val:
                   cur.next = cur.next.next
                   flag = True
              
                if not flag:
                    # if no duplicated value in this round of searching, proceed to the next one
                    virtual = virtual.next
                else:
                    # if there is duplicated value, change the next pointer
                    virtual.next = cur.next
                flag = False
    
            
            return dummy_head.next

Log in to reply
 

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