My one-pass accepted python code, beat 92%


  • 0
    L

    Maintain 3 pointers.

    class Solution(object):
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            dummy = ListNode(-1)
            dummy.next = head
            i = dummy
            j = dummy.next
            k = dummy.next.next if dummy.next else None
            while i and j and k:
                if j.val == k.val:
                    while k and j.val == k.val:
                        k = k.next
                    i.next = k
                    j = k
                    k = k.next if k else None
                else:
                    i = i.next
                    j = j.next
                    k = k.next
            return dummy.next
    

Log in to reply
 

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