My short python solution with comment


  • 0
    H
    class Solution:
        # @param head, a ListNode
        # @return a ListNode
        def deleteDuplicates(self, head):
            fh = ListNode(-1)
            prev = fh
            
            p = head
            while p:
                q = p.next
                while q and q.val == p.val:
                    q = q.next
                # q either is None or q.val != p.val
                if q == p.next:
                    # p is distinct
                    prev.next = p
                    p = p.next
                    prev = prev.next
                    prev.next = None
                else:
                    p = q
    
            return fh.next

Log in to reply
 

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