您的位置:首页 > 其它

leetcode Rotate List

2014-10-06 11:09 288 查看
此题主要在于理解旋转链表的含义

代码

class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if(head==NULL||k==0)
return head;

int len = 1;
ListNode* p = head;
while(p->next!=NULL)
{
p = p->next;
len++;
}

p->next = head;
int step = len - (k%len);

while(step>0)
{
p = p->next;
step--;
}

ListNode* newHead = p->next;
p->next = NULL;
return newHead;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: