Shift all zeroes to the end while keeping the relative order of non-zero elements.
Write a function `move_zeroes(nums)` that takes a list of integers `nums` and modifies the list **in-place** so that all zeros are moved to the end of the list, while the relative order of all non-zero elements is preserved. The function should return the modified list (which is the same list object as the input). Do not create a copy of the list; the modification must be done by rearranging elements in the original list.
Examples:
- `move_zeroes([0,1,0,3,12])` returns `[1,3,12,0,0]` and the input list becomes `[1,3,12,0,0]`.
- `move_zeroes([0,0,1])` returns `[1,0,0]`.
- `move_zeroes([1,2,3])` returns `[1,2,3]`.
- `move_zeroes([0,0,0])` returns `[0,0,0]`.
- `move_zeroes([])` returns `[]`.
Implement the function such that it performs the operation in O(n) time and O(1) extra space.
Constraints
- Input list length: 0 ≤ n ≤ 10^5.
- Each element is an integer.
- Must modify the list in-place.
- Time complexity O(n), extra space O(1).
Example
>>> nums = [0,1,0,3,12]
>>> move_zeroes(nums)
[1,3,12,0,0]
>>> nums
[1,3,12,0,0]
>>> nums = [0,0,1]
>>> move_zeroes(nums)
[1,0,0]
>>> nums
[1,0,0]
>>> nums = [1,2,3]
>>> move_zeroes(nums)
[1,2,3]
>>> nums
[1,2,3]
10 points
~15 min