class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if(head==NULL)
return NULL;
ListNode *p,*q,*qpre,*end;
q=p=head;
int count=0;
while(p!=NULL)
{
if(p->next==NULL)
end=p;
p=p->next;
count++;
}
if(k>=count)
k=(k-count)%count;
if(count-k==0||k==0)
return head;
for(int i=1;i<=count-k;i++)
{
qpre=q;
q=q->next;
}
qpre->next=NULL;
end->next=head;
return q;
}
};
不停地传,不停地错,写得乱七八糟
Rotate List
最新推荐文章于 2024-11-06 23:21:54 发布