题解 | #转动链表#
转动链表
https://www.nowcoder.com/practice/afbec6b9d4564c63b2c149ccc01c5678
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
*
* @param head ListNode类
* @param k int整型
* @return ListNode类
*/
ListNode* rotateRight(ListNode* head, int k)
{
if(head==NULL||k==0||head->next==NULL)
{
return head;
}
int length=0;
ListNode *po=head;
while (po->next)
{
length++;
po=po->next;
}
po->next=head;
length++;
for(int i=0;i<length-(k%length);i++)
{
po=po->next;
}
ListNode*res=po;
res=po->next;
po->next=NULL;
return res;
// write code he
}
};

