I have a sorted list. I want to move the values d
steps forwards. And I need O(n) time complexity.
For example if d = 2
, the value 1
, which was in index = 0
, now is in index = 2
:
lst = [1,2,3,4,5]
d = 2
Expected output: [4,5,1,2,3]
The code:
def cyclic_lst(lst,d):
mylist = []
counter = 0
while(counter <= d):
mylist = lst[1:] + lst[0]
counter += 1
return mylist
I'm getting this error:
TypeError: can only concatenate list (not "int") to list
I can't use for
loop with the while
loop because of the complexity demand, how can I do this?