def rotateRight(self, head, k):
if head is None:
return head
length=1 #count the length of the list
p=head
while p.next is not None:
length=length+1
p=p.next
k=k%length
if k==0:
return head
p=head # last node link to the old head
q=head # this will be the new end ,it's next will be the new head
while p.next is not None:
if k<=0:
q=q.next
p=p.next
k=k1
p.next=head
head=q.next
q.next=None
return head
O(n) python solution , need some suggestion to improve or make it cleaner


Here is my solution with the same question...
def rotateRight(self, head, k): if not head: return length = 0 current = head while current: length += 1 #current will point at the last node of initial list if current.next is None: break current = current.next k %= length if k == 0: return head current.next = head for i in range(length  k): #current will point at the last node of desired list current = current.next head = current.next current.next = None return head