Very easy 20 line Cpp Solution


#1

ListNode* Solution::rotateRight(ListNode* A, int B) {
if(!A || !B) return A;
int c = 0;
ListNode *it = A;
while(it) it = it->next,c++;
int r = B%c;
if(!r) return A;

r = c-r-1;
it = A;
while(r) it = it->next,r--;
ListNode *it2 = it->next;
ListNode *start = it2;
it->next = NULL;

while(it2->next) it2 = it2->next;

it2->next = A;
return start;

}