6 lines and O(n) Python solutiom


  • 0
    class Solution(object):
        def removeElement(self, nums, val):
            k = len(nums)-1
            for i in range(k,-1,-1):
                if nums[i] == val:
                    del nums[i]
                    k = len(nums)-1   
            return len(nums)
    

Log in to reply
 

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