Skip to content
Decoder

Move Zeroes

Easy

Problem

Given a list of integers nums, move every 0 to the end in place, keeping the non-zero values in their original relative order. Do not return anything and do not build a new list: change nums itself.

Example 1

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

Example 2

Input
nums = [0]
Output
nums stays [0]

Example 3

Input
nums = [4, 0, 5]
Output
nums becomes [4, 5, 0]

Constraints

  • 0 <= nums.length <= 10^4
  • -2^31 <= nums[i] <= 2^31 - 1
  • Use O(1) extra space.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.