/**
* 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)
{
if(head == NULL)
return head;
int length = 1;
ListNode* tail = head;
while(tail->next != NULL)
{
tail = tail->next;
++length;
}
int index = k%length;
if(!index)
return head;
int temp = length-index;
ListNode *cur = head;
while(temp != 1)
{
cur = cur->next;
--temp;
}
ListNode* newhead = cur->next;
cur->next = NULL;
tail->next = head;
return newhead;
}
};
Rotate List
最新推荐文章于 2015-10-14 20:58:50 发布