---
title: "ByteByteGo: Shift Zeros to the End"
url: https://perrotta.dev/2025/11/bytebytego-shift-zeros-to-the-end/
last_updated: 2026-01-03
---


[Same as LeetCode #283]({{< ref "2025-09-12-leetcode-283-move-zeroes" >}}).

[ByteByteGo: Shift Zeros to the End](https://bytebytego.com/exercises/coding-patterns/two-pointers/shift-zeros-to-the-end):

```python
from typing import List

def shift_zeros_to_the_end(nums: List[int]) -> None:
    j = None

    for i, num in enumerate(nums):
        if num != 0:
            if j is not None:
                assert j < i
                nums[i], nums[j] = nums[j], nums[i]
                j += 1
        else:
            if j is None:
                j = i

```

Simplified version:

```python
from typing import List

def shift_zeros_to_the_end(nums: List[int]) -> None:
    j = 0

    for i, num in enumerate(nums):
        if num != 0:
            nums[i], nums[j] = nums[j], nums[i]
            j += 1
```

