Shift All Zeros to the End of the Array

Implement moveZeroesToEnd

Given an array nums, return the array with every zero moved to the end, while every non-zero value keeps its original relative order. Try to do it by rearranging values in a single pass rather than building a second array — a two-pointer swap gets you there in O(1) extra space.

Example 1:

Input: nums = [0,5,0,3,9,0]

Output: [5,3,9,0,0,0]

Example 2:

Input: nums = [4,2,7]

Output: [4,2,7]

Example 3:

Input: nums = [0,0,0]

Output: [0,0,0]

+ 4 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 10⁵
  • -10⁹ ≤ nums[i] ≤ 10⁹

nums =

[0, 5, 0, 3, 9, 0]