Move Zeroes
Move Zeroes is LeetCode 283 (Easy). Given an integer array nums, move every 0 to the end of the array.
- The non-zero values must keep their original relative order.
- The change must happen in place: no copy of the array.
The follow-up asks to keep the number of writes to the array as low as possible.
- 1 <= nums.length <= 10⁴
- -2³¹ <= nums[i] <= 2³¹ - 1
Intuition
Think of the array as two zones: a finished front part holding the non-zero values seen so far, and everything after it. A write pointer marks where the front part ends, and a read pointer scans every slot once.
- Read finds a zero: skip it. It already belongs further back.
- Read finds a non-zero: swap it into the slot at write, then move write forward.
Every slot between write and read holds a zero, so each swap sends a zero backward and pulls a non-zero value forward. Values reach the front in the order they are read, which is why their order survives.
Rearrange an array in place so that values passing a test come first, in order: that is the read/write pointer pattern. Remove Element and Remove Duplicates from Sorted Array use the same two pointers, keeping a different kind of value.
Approach
Before reading on, decide what the slots between the two pointers always contain. Aim for one pass, O(1) extra space, and no writes when the array has no zeros.
Two ways to solve it
One pass; each non-zero value is swapped into the next front slot.
- Single pass: zeros end up at the back as a side effect.
- No writes for zeros: good when zeros are common.
Copy non-zero values to the front, then overwrite the rest with zeros.
- Simplest to write: no swaps.
- Two passes: every slot after the last value is written again.
Both keep the order and use no extra array. The swap version finishes in one pass, so the steps, code and live demo use it; the fill version follows the demo.
Start both pointers at the front
Set write = 0. Nothing is in its final place yet, so the first non-zero value should land in slot 0.
Scan with read and skip zeros
Move read from left to right over every slot. A zero is left where it is for now; write stays put, so it keeps pointing at the first zero that has to be pushed back.
Swap each non-zero value forward
When nums[read] is not zero, swap it with nums[write] and add 1 to write. If no zero has been seen yet, the two pointers are equal and the swap leaves the array unchanged.
Why the order is kept
Non-zero values are placed at write one at a time in reading order, and a value never moves again once placed. So the front holds them exactly as they appeared, and only zeros are left behind.
Code in Python, C++ and Java
Compact, then fill with zeros
The first loop copies each non-zero value to nums[write] and advances write, overwriting whatever was there; nothing is lost, because write never passes the value being read. When the loop ends, slots from write to the end must hold zeros, so a second loop writes them.
Common pitfalls
Swapping zeros with values from the far end
left, right = 0, len(nums) - 1
while left < right:
if nums[left] == 0:
nums[left], nums[right] = nums[right], nums[left]
right -= 1
else:
left += 1nums[write], nums[read] = nums[read], nums[write]
Pulling values in from the end puts zeros at the back but scrambles the order. [0, 1, 2] becomes [2, 1, 0], while the expected answer is [1, 2, 0].
Complexity
Move Zeroes FAQ
Which version does fewer writes to the array?
The swap version writes only when it meets a non-zero value, two writes per swap, and none for zeros. The fill version below writes every slot once. Arrays with many zeros favour the swap; arrays with few zeros favour the fill. Skipping the swap when read == write cuts writes further when the array starts with non-zero values.
How do I move the zeros to the front instead?
Run the same idea from the right: start write at the last index, scan read leftward, and swap each non-zero value into write, moving write left. The non-zero values then keep their order at the back.