Python answer for wiggle Sort


  • -1
    N
    class Solution(object):
        def wiggleSort(self,aList):
            newList=[]
            aList.sort()
            x=0
            y=0
            i=len(aList)//2+1
            while x<i:
                newList.insert(y,aList[x])
                x+=1
                y=x+2
            z=0
            while i<len(aList):
                newList.insert(z+1,aList[i])
                z+=2
                i+=1
            return newList
    

    Hi, this is my Python code. I tried it on my IDE and it works. However, when I test it here and I receive the output: "Do not return anything, modify nums in-place instead". Why does it happen? Can anyone help me?

    Thank you so much!


  • 1
    T

    You should modify the aList passed to you instead of returning a new list. One simple fix is to do

    for i, item in enumerate(newList):
        aList[i] = item

  • 1

    Or simply aList[:] = newList.


  • 0
    N

    Thank you :D


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.