Zeros to the Back

EasyTwo Pointers≈ 10 min

You are given a list of integers nums. Return the list with every zero moved to the end, and every other number kept in the order it had.

Examples

text
nums = [0, 3, 0, 5, 8]   →  [3, 5, 8, 0, 0]
nums = [1, 2, 3]         →  [1, 2, 3]
nums = [0, 0, 4]         →  [4, 0, 0]

Constraints

  • Between 0 and 10,000 numbers, each between -1,000,000 and 1,000,000.
  • Aim for O(n): place each number once.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 7 on the judge, 4 of them hidden, and measures how your solution scales.