61. Rotate List
找到新头前一个元素,重置指针,边界情况处理。
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return head;
//first pass, find length
int len=0;
ListNode *cur=head, *last=NULL;
while (cur) {
len++;
last=cur;
cur=cur->next;
}
k%=len;
if(k==0) return head;
//second pass, find cutoff point
ListNode *cutoff=head;
for (int i=0;i