### 解题思路

首先计算出链表的长度n,然后统计出要翻转的次数s = k % n

将链表后s个元素翻转,就可以实现倒序遍历

之后依次倒序遍历将队尾元素移动到队头

记得将结尾元素的next置空为0

### 代码

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        int n = 0;
        ListNode *p = head;
        while(p) p = p->next,n++;
        if(n <= 1) return head;
        ListNode *a = head;
        ListNode *mid = head;
        int s = k % n;    
        if(s == 0) return head;
        for(int i = 0; i < n - s;++i) mid = a,a = a->next;
        ListNode *b = a->next;
        for(int i = 0; i < s-1;++i){   //翻转后s个元素
            auto c = b->next;
            b->next = a;
            a = b,b = c;
        }
        ListNode* tail = a;
        for(int i = 0; i < s-1; ++i){  //移动元素
            auto c = tail->next;
            tail->next = head;
            head = tail;
            tail = c;
        }
        tail->next = head;
        head = tail;
        mid->next = nullptr;
        return head;
    }
};