class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (!head) return head;
auto p = head;
auto tail = head;
int n = 1;
while (tail->next) {
tail = tail->next;
n ++;
}
k %= n;
if (!k) return head;
auto q = head;
for (int i = 1; i <= n - k - 1; i ++) {
q = q->next;
}
tail->next = head;
head = q->next;
q->next = NULL;
return head;
}
};