Given the `head` of a linked list, rotate the list to the right by `k` places.

**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\]

**Constraints:**

*   The number of nodes in the list is in the range `[0, 500]`.
*   `-100 <= Node.val <= 100`
*   `0 <= k <= 2 * 109`
```python
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def rotateRight(head, k):
    if not head or not head.next or k == 0:
        return head

    current = head
    length = 1
    while current.next:
        length += 1
        current = current.next
    current.next = head
    k = length - k % length

    while k:
        k -= 1
        current = current.next
    head = current.next
    current.next = None

    return head
```
explanation
The algorithm first finds the length of the linked list by iterating through it. It then calculates the actual number of rotations needed, considering that if `k` is equal to or larger than the length of the linked list, the same rotation will be repeated. To minimize the rotations, we find the remainder of `k` divided by the length of the list.

Next, we connect the tail of the linked list to the head, forming a circular linked list. Following this, we iterate again through the list and perform the rotations by updating the pointers of the new head and tail. Finally, we break the cycle in the list and return the new head.