Given the head of a linked&nbsp;list, rotate the list to the right by k places.

&nbsp;
Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]


Example 2:

Input: head = [0,1,2], k = 4
Output: [2,0,1]


&nbsp;
Constraints:


	The number of nodes in the list is in the range [0, 500].
	-100 &lt;= Node.val &lt;= 100
	0 &lt;= k &lt;= 2 * 109


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        if k==0 or head is None or head.next is None:
            return head
        cur=head
        n=0
        while cur is not None:
            cur=cur.next
            n+=1
        k=n-k%n
        if k==n:
            return head
        cur=head
        prev=None
        while k>0 and cur is not None:
            prev=cur
            cur=cur.next
            k-=1
        prev.next=None
        prev=cur
        while cur.next is not None:
            cur=cur.next
        cur.next=head
        return prev
        
class Solution {
    public ListNode rotateRight(ListNode head, int k) {
        if(k<=0 || head==null || head.next==null){
            return head;
        }
        
        int length=1;
        ListNode first=head;
        ListNode curr=head;
        ListNode node=head;   
        while(node.next!=null){
            length++;
            node=node.next;
        }
        
        if(k==length){
            return head;
        }
        
        int n=length-(k%length);
        for(int i=0; i<n-1;i++){
            curr=curr.next;
        }
        
        node.next=head;  //5-->1
        head=curr.next;
        curr.next=null;
        
        return head;
    }
}
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
// like if it is useful to you
    ListNode* rotateRight(ListNode* head, int k) {
       if(head == NULL){
           return head;
       }
       vector<int> nums;
       ListNode *temp = head;
       while(temp != NULL){
           nums.push_back(temp->val);
           temp = temp->next;
       }
// if k greater than size;
        k = k%nums.size();
// rotating vector
     reverse(nums.begin(),nums.end());
     reverse(nums.begin(),nums.begin()+k);
     reverse(nums.begin()+k,nums.end());
// replace value of list    
 temp = head;
     for(int i = 0; i<nums.size();i++){
         temp->val = nums[i];
         temp = temp->next;
     }
     return head;
    }
};
var rotateRight = function(head, k) {
    if(k === 0 || !head) return head;

    let n = 0;
    let end = null;

    let iterator = head;
    while(iterator) {
        n += 1;
        end = iterator;
        iterator = iterator.next;
    }

    const nodesToRotate = k % n;
    if(nodesToRotate === 0) return head;

    let breakAt = n - nodesToRotate;
    iterator = head;
    while(breakAt - 1 > 0) {
        iterator = iterator.next;
        breakAt -= 1;
    }

    const newHead = iterator.next;
    iterator.next = null;
    end.next = head;

    return newHead;
};
Rotate List