Python | Simple One-Pass | O(n), O(1) | Beats 100%
CodeComplexity

Time complexity: O(n). Visits each index in nums, n total, and performs constant operations, so n overall.


Space complexity: O(1). Only variab
# Code
```python3 []
class Solution:
    def applyOperations(self, nums: List[int]) -> List[int]:
        n = len(nums)
        j = 0 # index of first zero
        for i in range(n):
            if i + 1 < n and nums[i] == nums[i + 1]: # check if should apply operation
                nums[i] *= 2 
                nums[i + 1] = 0

            if nums[i] != 0:
                nums[i], nums[j] = nums[j], nums[i] # swap zero and non-zero
                j += 1 # j + 1 for next zero index
                
        return nums
```

# Complexity
- Time complexity: $$O(n)$$. Visits each index in nums, n total, and performs constant operations, so n overall.

- Space complexity: $$O(1)$$. Only variables made are n and j, both constant space.

# Performance

![Screenshot (904).png](https://assets.leetcode.com/users/images/0e9ef281-026b-490d-be0e-df918b946d65_1740791151.600152.png)

['Two Pointers', 'Python3']