/**
* 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:
ListNode* rotateRight(ListNode* head, int k) {
if(!head)return head;
int size=0;
ListNode* no=head;
while(no)
{
no=no->next;
size++;
}
k%=size;
if(!k)return head;
ListNode* first=head;
for(int i=0;i<k;i++)first=first->next;
ListNode* second=head;
while(first->next)
{
first=first->next;
second=second->next;
}
first->next=head;
head=second->next;
second->next=nullptr;
return head;
}
};