leetcode Rotate List

Rotate List

class Solution {
public:
    ListNode *rotateRight(ListNode *head, int k)
    {
        if(head==NULL)return NULL;
        ListNode *p=head;
        int n=0;
        while(p->next)
        {
            p=p->next;
            n++;
        }
        n++;
        k=k%n;
        p->next=head;
        ListNode *q=head;
        for(int i=0;i<n-k-1;i++)
        q=q->next;
        head=q->next;
        q->next=NULL;    
        return head;
    }
};

 

posted @ 2013-05-19 20:32  代码改变未来  阅读(330)  评论(0编辑  收藏  举报