I wish to bring that last k elements of 'nums' array to the first. Like, Input:
nums = [1,2,3,4,5,6,7], k = 3
Output:
[5,6,7,1,2,3,4]
I have the following code:
class Solution(object):
def rotate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place instead.
"""
n = len(nums)
k = k % n
nums[:] = nums[n-k:] + nums[:n-k]
This works perfectly fine i.e. brings the last k elements to the beginning and nums shows [5,6,7,1,2,3,4]. But once I type the following nums = nums[n-k:] + nums[:n-k], it shows the resultant nums array as being the same original [1,2,3,4,5,6,7].
My question is, why is the change in output taking place? On googling and upon reding certain other threads in this forum pertaining to 'List Referencing and copying', I could realize that nums = is something about list referencing but nums[:] is like a mere copy of the list. But having said thiat, why is this change in output taking place? What's happening inside both the commands?
Seems like nums and nums[:] aren't yet clear to me. Please help.

