您的位置:首页 > 其它

leetcode 61: Rotate List

2015-08-02 15:41 316 查看
/**
* 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) {
ListNode* curr1=head;
if(curr1==NULL||curr1->next==NULL)
return head;
int size=1;
while(curr1->next)
{
curr1=curr1->next;
size++;
}
k%=size;
while(k--)
{
ListNode* curr2=head;
while(curr2->next!=curr1)
curr2=curr2->next;
curr2->next=NULL;
curr1->next=head;
head=curr1;
curr1=curr2;
}
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: