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 Wiggle Sort II problem solution

YASH PAL, 31 July 202422 January 2026

In this Leetcode Wiggle Sort II problem solution You are given an integer array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]…. You may assume the input array always has a valid answer.

Leetcode Wiggle Sort II problem solution

Leetcode Wiggle Sort II problem solution in Python.

class Solution:
    def wiggleSort(self, nums: List[int]) -> None:
        nums.sort()
        for i in range(len(nums)//2):
            nums.insert(len(nums)//2 - i - 1,nums.pop())
        if len(nums) % 2 != 0:
            nums.insert(0,nums.pop())
        nums.reverse()
        if len(nums)>1 and nums[-1] == nums[-2]:
            for i in range(len(nums)):
                if nums[i] < nums[-2] and nums[-1] < nums[i+1]:
                    nums[i],nums[-1] = nums[-1],nums[i]
                    break

Wiggle Sort II problem solution in Java.

class Solution {
    public void wiggleSort(int[] nums) {
        int[] tempArr=Arrays.copyOf(nums,nums.length);
        Arrays.sort(tempArr);
        int t=tempArr.length-1;
        for(int i=0;i<nums.length;i++){
            if(i%2!=0){
                nums[i]=tempArr[t];
                tempArr[t]=-1;
                --t;
            } 
        }
        t=0;
        int k=0;
        for(int i=nums.length-1;i>=0;i--){
            if(i%2==0){
                nums[i]=tempArr[t];
                t++;
            }
        }
    }
}

Problem solution in C++.

class Solution {
public:
    void wiggleSort(vector<int>& nums) {
        vector<int> sorted(nums);
        sort(sorted.begin(),sorted.end());
        int idx = sorted.size()-1;

        for(int i=1; i<nums.size(); i+=2) nums[i] = sorted[idx--];

        for(int i=0; i<nums.size(); i+=2) nums[i] = sorted[idx--];
        
        return;
    }
};

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