Solution that beats 99.02% submissions.


  • 0
    M
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if head is None or head.next is None:
            return head
        
        dummyHead = ListNode(0)
        dummyHead.next = head
        prev = dummyHead
        node = head
        cur = head.next
        while cur:
            if cur.val != node.val:
                if node.next is cur:
                    prev = node
                    node = node.next
                else:
                    prev.next = cur
                    node = cur
            cur = cur.next
        if not node.next is cur:
            prev.next = None
        return dummyHead.next
    

Log in to reply
 

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