---
# System prepended metadata

title: 'Move Zeroes <img src="https://img.shields.io/badge/-easy-brightgreen">'
tags: [data_structure_python]

---

---
tags: data_structure_python
---

# Move Zeroes <img src="https://img.shields.io/badge/-easy-brightgreen">

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

<ins>**Example:**</ins>

```
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
```

**Note:**

- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.


# Solution

### Solution 1: O(n) in time/space complexity

```python=
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        count = 0
        i = 0
        while i < len(nums):
            if nums[i] == 0:
                count += 1
                nums.pop(i)
                i -= 1
            i+= 1
        nums += [0]*count 
```

### Solution 2: O(n) in time complexity / O(1) in space complexity

```python=
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        n = len(nums)
        pointer1 = 0
        for pointer2 in range(n):
            if nums[pointer2] != 0:
                nums[pointer1] = nums[pointer2]
                pointer1 += 1
        
        for i in range(pointer1, n):
            nums[i] = 0
```