All problems

Move Zeroes

Easy · two pointers

A shelf holds items labelled with whole numbers. A label of 0 marks an empty box. Tidy the shelf by pushing every empty box to the right-hand end, while all the other items keep the same left-to-right order they had.

Given nums, return the tidied list.

Examples

Example 1

Input: nums = [0, 4, 0, 5, 6]

Output: [4, 5, 6, 0, 0]

The non-zero values 4, 5, 6 keep their order and both zeros finish at the end.

Example 2

Input: nums = [0, 0, 9]

Output: [9, 0, 0]

Example 3

Input: nums = [3, 1, 2]

Output: [3, 1, 2]

Constraints