Two Pointers· 15 min

Move Zeroes

LeetCode #283 · Easy · Interactive step-by-step visualization

Setup

Example inputnums = [0, 1, 0, 3, 12]
Step 1 / 12

Current step

1 / 12

Read 0 at index 0. The read pointer searches for the next non-zero item. Zero stays pending.

Step log

Active cursor
Comparing
Best or completed
Leaving or removed
move-zeroes.java
1public void moveZeroes(int[] nums) {
2 int write = 0;
3 for (int read = 0; read < nums.length; read++) {
4 if (nums[read] != 0) {
5 int temp = nums[write];
6 nums[write++] = nums[read];
7 nums[read] = temp;
8 }
9 }
10}
● Java
Time
O(n)
Space
O(1)
Pattern
two pointers
Frames
12