Python Simple Solution

  • 1

    curr will always be the node behind, in case the next node is a duplicate that needs to be removed. Even the head can be removed. Hence having a dummy node solves this problem.

    - Yangshun

    class Solution(object):
        def deleteDuplicates(self, head):
            :type head: ListNode
            :rtype: ListNode
            # Time: O(n)
            # Space: O(1)
            dummy = ListNode(None)
   = head
            curr = dummy
            while curr:
                has_dup = False
                # Remove duplicates and leave the last of the duplicates.
                while and and ==
                    has_dup = True
                if has_dup:
                    # Remove the last duplicate
                    curr =

Log in to reply

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