iq.lab
Python starts when a code cell comes near or you run one
easyTwo pointers target 15 min

Move zeroes to the end

You get a list of integers nums. Rearrange it in place (inside the same list, using only a few extra variables) so that every 0 ends up at the end, and the other numbers stay in the order they had.

The function returns nothing. The tests call it and then look at nums.

Example 1
Inputnums = [0, 4, 0, 9, 2]Outputnums becomes [4, 9, 2, 0, 0]

4, 9 and 2 keep their order. The two 0s move to the end.

Example 2
Inputnums = [3, 8]Outputnums stays [3, 8]

There are no 0s, so nothing moves.

Example 3
Inputnums = [0, 0, -1]Outputnums becomes [-1, 0, 0]

-1 is not 0, so it moves to the front.

Constraints
  • 0 ≤ len(nums) ≤ 106

  • -109 ≤ nums[i] ≤ 109

Plan it first

Write a line for each before you code, then say them out loud. Compare with the Approach tab afterwards.

⌘+Enter runs 0:00Python starts when a code cell comes near or you run one
Run examples checks the examples. Submit runs every test, including edge cases and, when the problem has one, a speed check on a large input.