image.png```
    把链表的尾部和头部连一起,方便查找
    然后根据位置的关系找出来新的尾部,新的尾部的下一个就是新的头部

    1. /**
    2. * Definition for singly-linked list.
    3. * struct ListNode {
    4. * int val;
    5. * ListNode *next;
    6. * ListNode(int x) : val(x), next(NULL) {}
    7. * };
    8. */
    9. class Solution {
    10. public:
    11. ListNode* rotateRight(ListNode* head, int k) {
    12. if(!head) return head;
    13. auto h = head, tail = head;
    14. int len = 0;
    15. while(tail->next)
    16. {
    17. tail = tail->next;
    18. len++;
    19. }
    20. len++;
    21. k = k % len;
    22. if(k == 0) return head;
    23. tail->next = h;
    24. int move = len - k - 1;//向前移动idx找tail
    25. tail = h;
    26. while(move--)
    27. tail = tail->next;
    28. h = tail->next;
    29. tail->next= nullptr;
    30. return h;
    31. }
    32. };