# Description

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.

The original problem is here.

The original code is here.

# My Solution

I solve this problem in C++, as below:

``````/*
*Rotate List
*Author: shuaijiang
*Email: zhaoshuaijiang8@gmail.com
*/
#include<iostream>
#include<math.h>
#include<vector>
#include<stdlib.h>
using namespace std;

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
int nodeSize = 0;
vector<ListNode*> vec;

while(node!=NULL){
vec.push_back(node);
node=node->next;
}
nodeSize = vec.size();
k = k % nodeSize;
if(k==0)

for(int i=0;i<k;++i){
node = vec[nodeSize-1-i];
if(i==0)
node->next = vec[0];
else
}
node = vec[nodeSize-1-k];
node->next = NULL;
}
};
``````

# Note

If k is larger than the number of nodes, we need to modular k by the number of nodes.