Python Simple O(n) Time O(1) space

  • 0

    Similar to Remove Duplicates from Sorted List I (duh) but now we just need to check against both the previous value as well as the next value. Keep track of previous value in a variable and use that to check if duplicate or not.

        def deleteDuplicates(self, head):
            buffr = prev = ListNode(None)
   = itr = head
            val = None
            while itr:
                nxt =
                if itr.val == val or ( and == itr.val):
          , =, None
                    prev = itr
                itr, val = nxt, itr.val # move pointer forward and update previous val

Log in to reply

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