LeetCode 61. 旋转链表
原题链接
中等
作者:
linux_2019
,
2019-05-23 15:53:29
,
所有人可见
,
阅读 1019
C++ 代码
/**
* 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) {
int N=1;
ListNode *first=head,*second=head;
if(!head)
return head;
while(first->next)
{
N++;
first=first->next;
}
k=k%N;
k=N-k;
first->next=head;
first=head;
while(--k)
first=first->next;
second=first->next;
first->next=NULL;
return second;
}
};