c++; simple solution


  • 0
    S
        ListNode* rotateRight(ListNode* head, int k) {
            if(k==0 || !head || !head->next)return head;
            vector<ListNode*> ans;
            ListNode* cur = head;
            while(cur !=NULL){ans.push_back(cur); cur=cur->next;}
            k = k % ans.size();
            if(k==0)return head;
            ListNode* newTail = ans[ans.size()-k-1];
            ListNode* tmpHead = ans[ans.size()-1];
            ListNode* newHead = ans[ans.size()-k];
            newTail->next = tmpHead->next;
            tmpHead->next = head;
            head = newHead;
            return head;
        }
    
    

Log in to reply
 

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