Given a list of integers nums, rearrange it in place so that every 0 comes first and the non-zero elements follow, keeping their original relative order. The function returns nothing; the list itself must be changed.
Tests call it as (zeros_to_front(a := [1, 0, 2, 0]), a)[1] and inspect the list afterwards.
Examples
Input: nums = [1, 0, 2, 0]
After: [0, 0, 1, 2]
Input: nums = [0, 3, 0, 4, 5]
After: [0, 0, 3, 4, 5]
Constraints
0 <= len(nums) <= 10**5- Must modify
numsin place and returnNone. - Target: O(n) time and O(1) extra space (no second list).
Goals
- Modify a list in place using a write pointer
- Scan from the back so the non-zero order is preserved
- Use O(1) extra space