您的位置:首页 > 其它

Rotate List

2014-02-20 13:09 204 查看
/**
* 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 || k<0)	return 0;
int length=0;
ListNode *pCur=head;
ListNode *pTail=0;
while(pCur)
{
length++;
if(!pCur->next)
{
pTail=pCur;
}
pCur=pCur->next;
}
pTail->next=head;
int index=length-k%length;
pCur=head;
for(int i=1;i<index;i++)
{
pCur=pCur->next;
}
head=pCur->next;
pCur->next=0;
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  链表