My fast and clean solution towards Rotate List beating 95% cpp submissions


  • 0
    J

    With a clean and understandable solution towards Rotate List using pointers as the breakpoints of the problem.

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

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.