Move Zeroes

Two Pointers, problem 2 of 7

Move Zeroes

Easy

LC #283

in placeread/write pointers

Not attempted yet

Move every 0 in nums to the end while keeping the relative order of the non-zero values.

Do it in place: modify nums directly and don't return anything (the tests inspect nums after your call).

Example 1

Input: nums = [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]

Example 2

Input: nums = [0]
Output: [0]

Example 3

Input: nums = [4, 0, 0, -2, 5]
Output: [4, -2, 5, 0, 0]

Constraints

  • 1 ≤ len(nums) ≤ 10⁵
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1
  • O(1) extra space; don't build a new list

Python

Loading draft…

Test results

8 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 5 hidden

Run examples, then submit all tests.