1、题目
https://leetcode-cn.com/problems/rotate-list/submissions/
2、题意
题解1:
1 2 3 4 5 k = 2;
将在5的指针指向开头1 head = 4 在将4的节点定义为nullptr
即4->5->1->2->3->null;
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (!head) return head;
int n = 0;
ListNode* tail;
for (auto p = head; p; p = p->next) {
tail = p;
n ++;
}
k %= n;
if (!k) return head;
ListNode* p = head;
for(int i=1;i<=n-k-1;k++)
p = p->next;
tail->next = head;
head = p->next;
p->next = nullptr;
return head;
}
};