113. Move Zeroes

Easy · Array

Given an array of integers, move all zeroes to the end of the array while maintaining the relative order of non-zero elements. You must do this in-place with O(1) extra space.

Modify the input array directly and return it. Do not create a new array.

Examples

Example 1
Input: [0, 1, 0, 3, 12]
Output: [1, 3, 12, 0, 0]
Explanation: All non-zero elements (1, 3, 12) maintain their relative order, and the two zeroes are moved to the end.
Example 2
Input: [0]
Output: [0]
Explanation: A single zero remains in place.

Constraints