Quite simple 9ms C++ code. Use vector. LOL.


  • 0
    D
    class Solution {
    public:
        ListNode* rotateRight(ListNode* head, int k) {
            ListNode *start=new ListNode(0);
    		start->next=head;
            if(head==NULL) return head;
    		vector<ListNode *> tmp;
    		while(head!=NULL)
    		{
    			tmp.push_back(head);
    			head=head->next;
    		}
    		if(k>=tmp.size())
    		{
    			k=(k)%tmp.size();
    		}
    		if(k==0) return start->next;
    		tmp[tmp.size()-1]->next=start->next;
    		start->next=tmp[tmp.size()-k];
    		tmp[tmp.size()-k-1]->next=NULL;
    		
    		return start->next;
        }
    };
    

Log in to reply
 

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