Move all zeroes in a list of integers to the end while keeping the relative order of the non-zero elements. Return a new list with the result. Return null for null input and an empty list for empty input.

Examples:

Input: nums = [0, 1, 0, 3, 12] Output: [1, 3, 12, 0, 0] Explanation: Non-zero elements keep their order, zeroes move to the end Input: nums = [0, 0, 1] Output: [1, 0, 0] Explanation: The single non-zero element moves to the front, zeroes follow Input: nums = [1, 2, 3] Output: [1, 2, 3] Explanation: No zeroes present, list is unchanged Input: nums = null Output: null Explanation: Null input returns null Input: nums = [] Output: [] Explanation: Empty list returns empty list
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay September 2026 (Sep 16–30)Sponsored by FLXBL

Contest runs September 16 - 30. Complete challenges to climb the leaderboard!

Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In