Efficient way to rotate a list in python

后端 未结 26 1836
一生所求
一生所求 2020-11-22 03:14

What is the most efficient way to rotate a list in python? Right now I have something like this:

>>> def rotate(l, n):
...     return l[n:] + l[:n]         


        
26条回答
  •  面向向阳花
    2020-11-22 03:47

    I was looking for in place solution to this problem. This solves the purpose in O(k).

    def solution(self, list, k):
        r=len(list)-1
        i = 0
        while i

提交回复
热议问题