Rotate List

来源:互联网 发布:神偷4优化补丁 编辑:程序博客网 时间:2024/05/20 20:02
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;    }};
不停地传,不停地错,写得乱七八糟
0 0
原创粉丝点击