Rotate List
class Solution {
public:
int length(ListNode* head){
int len = 0;
for(auto node = head; node != nullptr;node = node->next, len ++);
return len;
}
ListNode *at(ListNode* node, int idx){
auto _node = node;
for(int i = 0; i< idx;i++, _node = _node->next);
return _node;
}
ListNode *rotateRight(ListNode *head, int k) {
if(head == nullptr) return nullptr;
int len = length(head);
int cut = (len - 1) - (k % len);
auto cuttingPoint = at(head, cut);
auto last = at(head, len - 1);
last->next = head;
auto res = cuttingPoint->next;
cuttingPoint->next = nullptr;
return res;
}
};