Rotate List

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head == NULL)
return NULL;
vector<ListNode* > queue;
ListNode* p = head;
while(p!=NULL)
{
queue.push_back(p);
p = p->next;
}
k = k%queue.size();
if(k==0)
return head;
queue[queue.size()-k-1]->next = NULL;
queue[queue.size()-1]->next = queue[0];
return queue[queue.size()-k];
}
};

原文地址:https://www.cnblogs.com/727713-chuan/p/3302375.html