class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(head == nullptr) return head;
ListNode *temp = head;
int length = 0;
while(temp != nullptr){
length++;
temp = temp->next;
}
k = k%length;
if(k == 0) return head;
ListNode *temp1 = head, *temp2 = head;
while(k--) temp2 = temp2->next;
while(temp2->next != nullptr){
temp1 = temp1->next;
temp2 = temp2->next;
}
temp2->next = head;
head = temp1->next;
temp1->next = nullptr;
return head;
}
};