Exchange element = val with the last element of the list, and pop the last element out.
classSolution(object):defremoveElement(self,nums,val):""" :type nums: List[int] :type val: int :rtype: int """ i =0while i <len(nums):if nums[i]== val: temp = nums[i] nums[i]= nums[-1] nums[-1]= temp nums.pop()if i <len(nums)and nums[i]!= val: i +=1returnlen(nums)