Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Move Zeroes problem solution

YASH PAL, 31 July 202420 January 2026

In this Leetcode Move Zeroes problem solution, we have given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

Leetcode Move Zeroes problem solution

Leetcode Move Zeroes problem solution in Python.

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        for i in range(len(nums)):
            if(nums[i]==0):
                nums.remove(nums[i])
                nums.append(0)
            else:
                continue

Move Zeroes problem solution in Java.

class Solution {
    public void moveZeroes(int[] nums) {
        int idx = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != 0) {
                nums[idx] = nums[i];
                idx++;
            }
        }
        for (; idx < nums.length; idx++) {
            nums[idx] = 0;
        }
    }
}

Problem solution in C++.

class Solution {
public:
    void moveZeroes(vector<int>& nums) {
        
        int n = nums.size();
        int l = 0, r = 0;
       while(r < n && l < n) {
           while(l < n - 1 && nums[l] != 0) {
               ++l;
           }
            while(r < n- 1 && (r <= l || nums[r] == 0)) {
               ++r;
           }
           swap(nums[l++], nums[r++]);
       }
    }
};

Problem solution in C.

void moveZeroes(int* nums, int numsSize){
    int p1 = 0, p2 = 0;
    
    while(p2 < numsSize){
        while(p2 < numsSize && nums[p2] == 0) p2++;
        if(p2 < numsSize && p1 < p2){
            nums[p1] = nums[p2];
            nums[p2] = 0;
        }
        p1++;
        p2++;
    }
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes