c++ simple solution


  • 0
    Z
    class Solution {
    public:
        ListNode* rotateRight(ListNode* head, int k) {
            if(!head||!k||!head->next) return head;
            auto t1=head,t2=head,c=head;
            int n=0;
            while(c) n++,c=c->next;
            k%=n;
            if(!k) return head;
            while(k--) t1=t1->next;
            while(t1->next) t1=t1->next,t2=t2->next;
            auto res=t2->next;
            t2->next=NULL;
            t1->next=head;
            return res;
        }
    };

Log in to reply
 

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