给定一个链表,旋转链表,使得每个节点向右移动k个位置,其中k是一个非负数
样例
分析:链表的基本操作
给出链表1->2->3->4->5->null和k=2
返回4->5->1->2->3->null
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param head: the list
* @param k: rotate to the right k places
* @return: the list after rotation
*/
ListNode *rotateRight(ListNode *head, int k) {
if(head==nullptr)
return head;
// write your code here
ListNode*temp = head;
int len = 1;
while(temp->next!=nullptr)
{
temp = temp->next;
len++;
}
temp->next = head;
k %= len;
ListNode*cur = head;
int cnt = 1;
while(cnt<len-k)
{
cur = cur->next;
cnt++;
}
ListNode*ret = cur->next;
cur->next = nullptr;
return ret;
}
};