> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/two-pointers/move-zeros.md).

# Move Zeros

Two pointers

## Problem

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.

{% hint style="info" %}
For example:

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

{% endhint %}

## Solution

```
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:

        i = 0
        j = 0 #pointer position for putting the non-zeroes
        
        while i < len(nums):
            if nums[i] != 0:
                t = nums[j]
                nums[j] = nums[i]
                nums[i] = t
                j+=1
            i+=1
        
```
