# Rotate List Leetcode Solution In this post, you will know how to solve the Rotate List Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Leetcode by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## Problem

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`

### Rotate List Leetcode Solutions in Python

```class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
length = 1
while tail.next:
tail = tail.next
length += 1
tail.next = head  # Circle the list
t = length - k % length
for _ in range(t):
tail = tail.next
tail.next = None
```

### Rotate List Leetcode Solutionsin CPP

```class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
ListNode* tail;
int length = 1;
for (tail = head; tail->next; tail = tail->next)
++length;
tail->next = head;  // Circle the list
const int t = length - k % length;
for (int i = 0; i < t; ++i)
tail = tail->next;
tail->next = nullptr;
}
};
```

### Rotate List Leetcode Solutions in Java

```class Solution {
public ListNode rotateRight(ListNode head, int k) {
if (head == null || head.next == null || k == 0)
int length = 1;
for (; tail.next != null; tail = tail.next)
++length;
tail.next = head; // Circle the list
final int t = length - k % length;
for (int i = 0; i < t; ++i)
tail = tail.next;
tail.next = null;
}
}
```

Note: This problem Rotate List is generated by Leetcode but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.