Move all zeros to the end
Coding problemEasyAsked in: Amazon, Bloomberg, LinkedIn, Microsoft, Paytm, Samsung
Given a list of integers, move every 0 to the end of the list. The other values must keep their original order. Return the rearranged list.
The largest hidden tests have 300,000 values, so shifting the rest of the list every time you meet a zero is too slow.
Examples
Example 1
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
Example 2
Input: nums = [0]
Output: [0]
Example 3
Input: nums = [4,-2,0,0,7]
Output: [4,-2,7,0,0]
Constraints
- The list holds between 0 and 300,000 integers
- Each value is a 32-bit integer
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.