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 Next Permutation problem solution

YASH PAL, 31 July 202418 January 2026

In this Leetcode Next Permutation problem solution Implement the next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). The replacement must be in place and use only constant extra memory.

Leetcode Next Permutation problem solution

Leetcode Next Permutation problem solution in Python.

class Solution(object):
    def nextPermutation(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        
        lenn = len(nums)
        
        if lenn < 2:
            return
        
        a = [nums[-1]]
        
        for i in range(lenn-2,-1,-1):
            if nums[i] < nums[i+1]:
                m = i+1
                for j in range(i+1,lenn):
                    if nums[j] > nums[i]:
                        if nums[j] < nums[i+1]:
                            m = j
                
                nums[i], nums[m] = nums[m], nums[i]
                
                nums[i+1:] = sorted(nums[i+1:])
                
                return
        
        for i in range(lenn//2):
            nums[i],nums[lenn-1-i] = nums[lenn-1-i],nums[i]

Next Permutation problem solution in Java.

public static void nextPermutation(int[] nums) {
            if (nums == null || nums.length == 0) return;

            int maxElementIndex = nums.length-1;
            for (int i=nums.length-2;i>=0;i--) {
                if (nums[maxElementIndex]< nums[i]) maxElementIndex = i;
                if(nums[i] < nums[i+1]) {
                    for (int j=nums.length-1;j>i;j--) {
                        if (nums[j] > nums[i]){
                            maxElementIndex = j;
                            break;
                        }
                    }
                    swap(nums, i, maxElementIndex);
                    Arrays.sort(nums, i+1, nums.length);
                    return;
                }
            }

            Arrays.sort(nums);
        }

        private static void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }

Problem solution in C++.

class Solution {
public:
    void nextPermutation(vector<int>& nums) {
        int i = nums.size()-1;
        if(nums.size()==1) return;
        while(i>0 && nums[i] <= nums[i-1]) i--;
        if(i != 0){
            for(int j=nums.size()-1;j>=i;j--){
                if(nums[j]>nums[i-1]){
                    swap(nums[j],nums[i-1]);
                    break;
                } 
            }    
        }
        int j = nums.size()-1;
        while(i<j) swap(nums[i],nums[j]),i++,j--;
        
    }
};

Problem solution in C.

void nextPermutation(int* nums, int numsSize)
{
    int i, j;
    int t = -1;
    for (i = numsSize - 1; i > 0; --i) {
        if (nums[i - 1] < nums[i]) {
            t = i - 1;
            break;
        }
    }
    for (i = t + 1, j = numsSize - 1; i < j; ++i, --j) {
        nums[i] ^= nums[j];
        nums[j] ^= nums[i];
        nums[i] ^= nums[j];
    }
    if (t >= 0) {
        for (i = t + 1; i < numsSize; ++i) {
            if (nums[i] > nums[t]) {
                nums[i] ^= nums[t];
                nums[t] ^= nums[i];
                nums[i] ^= nums[t];
                break;
            }
        }
    }
}

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