您的位置:首页 > 其它

Rotate List

2014-03-08 14:13 162 查看
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;
}
};
不停地传,不停地错,写得乱七八糟
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: