[py3, js] 6 different simple solutions
Hi! There are many O(N**2) soultion, and some O(N) soultion.Brute Force
Repeat pop and insert at front k times.
Time complexity: O(NK)
Space complexity: O(1)

T
Hi! There are many O(N`**`2) soultion, and some O(N) soultion.


## Brute Force
- Repeat `pop` and `insert at front` k times.
- Time complexity: **O(NK)**
- Space complexity: **O(1)**
```Python
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        while k:
            nums.insert(0, nums.pop())
            k -= 1
```

- Time complexity: **O(NK)**
- Space complexity: **O(1)**
```JavaScript
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void}
 */
const rotate = (nums, k) => {
    while(k--) {
        nums.unshift(nums.pop());
    }
};
```
## `prepend` of Deque
- add element in front of deque takes constant time.  
- Time complexity: **O(K)**
- Space complexity: **O(N)**
```Python
from collections import deque

class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        d = deque(nums)
        for _ in range(k):
            d.appendleft(d.pop())
        nums[:] = list(d)
```

## Stack
- Time complexity: **O(K`**`2)**
- Space complexity: **O(N)**
```JavaScript
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void}
 */
const rotate = (nums, k) => {
    const stack = [];
    k %= nums.length;
    while(k--) {
        stack.push(nums.pop());
    }
    nums.unshift(...stack.reverse());
};
```

## Stack with reversing (RECOMMENDED)
- Time Complexity: **O(N)**
- Space Complexity: **O(N)**
```JS
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void}
 */
const rotate = (nums, k) => {
    const K = k % nums.length;
    const stack = []
    for (let _ = 0; _ < K; _++) {
        stack.push(nums.pop())
    }
    nums.reverse()
    nums.push(...stack)
    nums.reverse()
};
```

## Reduce `k`: Using Cyclic Replacements
_1. Remove redundant loop:_
  If `k` is bigger than length of `nums`. Bruteforce algorithm gonna looping `nums` over and over.
  To prevent the redunant work only move remainder of ` k / length of nums` steps.
- Time complexity: **O(NK)**
- Space complexity: **O(1)**
```Python
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        k = k % len(nums)
        while k:
            nums.insert(0, nums.pop())
            k -= 1
```
_2. Insert(0,x) takes O(N), swap elements takes O(1):_
  Python list is a dynamic array. inserting an element in front of array takes O(N).
   swaping element in array takes O(1). 
- Time complexity: **O(N)**
- Space complexity: **O(1)**
```Python
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        k %= len(nums)
        count, start = 0, 0
        while count < len(nums):
            current = start
            prev = nums[start]
            while 1:
                current = (current + k) % len(nums)
                temp, nums[current] = nums[current], prev
                prev = temp
                count += 1
                if start == current:
                    break
            start += 1
```
- Time complexity: **O(N)**
- Space complexity: **O(1)**
```JavaScript
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void} Do not return anything, modify nums in-place instead.
 */
var rotate = (nums, k) => {
    let [start, count] = [0, 0];
    while (count < nums.length) {
        let [current, prev] = [start, nums[start]];
        do {
            current = (current + k) % nums.length;
            [nums[current], prev] = [prev, nums[current]];
            count++;
        } while (current !== start)
        start++
    }
};
```

##  Reduce `k` & Use Slicing
- Time complexity: **O(N)**
- Space complexity: **O(N)**
- Python slice create new array
```Python
class Solution:
    def rotate(self, nums: List[int], k: int) -> None:
        k = k % len(nums)
        if k == 0: return
        nums[:-k], nums[-k:] = nums[-k:], nums[:-k]
```
- Time complexity: **O(N`**`2)**
- Space complexity: **O(N)**
- JavaScript splice create new array
```JavaScript
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {void}
 */
const rotate = (nums, k) => {
    nums.unshift(...nums.splice(nums.length - k%nums.length));
};
```

['Python', 'JavaScript']