What to do when k is greater than size of list ?

Let's start with an example.
Given
[0,1,2]
, rotate 1 steps to the right >[2,0,1]
.Given
[0,1,2]
, rotate 2 steps to the right >[1,2,0]
.Given
[0,1,2]
, rotate 3 steps to the right >[0,1,2]
.Given
[0,1,2]
, rotate 4 steps to the right >[2,0,1]
.So, no matter how big K, the number of steps is, the result is always the same as rotating
K % n
steps to the right.

This is my code, Given two pointers, one is faster than the other by k steps ,when k>n I just reset from head.
ListNode *rotateRight(ListNode *head, int k) { if(head==NULL) return NULL; ListNode* slow=head; ListNode* fast=head; int cnt=0; while(cnt<k) { if(!fast)fast=head; fast=fast>next; cnt++; } if(fast==NULL) return head; while(fast>next){ fast=fast>next; slow=slow>next; } fast>next=head; head=slow>next; slow>next=NULL; return head; }

{
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return NULL;
ListNode *p=head;
int i=0;
while(i!=k&&p!=NULL){
p=p>next;
i++;
}
if(p!=NULL){
ListNode *q=head;
while(p>next!=NULL){
p=p>next;
q=q>next;
}
p>next=head;
head=q>next;
q>next=NULL;
return head;} else{ k%=i; return rotateRight(head,k); } }
}; // if k is larger than length, k%=i return return rotateRight(head,k);
}

@rforritz I think if k > list's size, what we should do is rotate the list with
k % lists' size
steps.

