思路:将链表数据放到数组中,将数组旋转,然后再赋值给链表
struct ListNode* rotateRight(struct ListNode* head, int k) {
if(head==NULL)
{
return NULL;
}
int count=0;
struct ListNode*good=head;
while(good)
{
count++;
good=good->next;
}
int round;
if(k>=count)
{
round=k%count;
}
else
{
round=k;
}
int arr[1000];
int i=0;
struct ListNode*point=head;
while(point)
{
arr[i]=point->val;
i++;
point=point->next;
}
int arr1[1000];
int j=0;
int z=count-round;
for(int x=0;x<round;x++)
{
arr1[j]=arr[z];
j++;
z++;
}
for(int x=0;x<count-round;x++)
{
arr1[j]=arr[x];
j++;
}
struct ListNode*point1=head;
for(int x=0;x<count;x++)
{
point1->val=arr1[x];
point1=point1->next;
}
return head;
}