LeetCode 61. Rotate List
Posted dacc123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 61. Rotate List相关的知识,希望对你有一定的参考价值。
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)
if(head==NULL)
return head;
if(k==0)
return head;
ListNode* end ;
ListNode* start = head;
ListNode* pre;
ListNode* term =head;
int len=1;
while(head->next!=NULL)
head = head->next;
len++;
if(len==1)
return head;
end = head;
int n = k%len;
if(n==0)
return start;
int m = len-n;
head = start;
int i=0;
while(head!=NULL)
if(i==m)
start = head;
break;
if(i==m-1)
pre=head;
i++;
head=head->next;
pre->next=NULL;
end->next=term;
return start;
;
以上是关于LeetCode 61. Rotate List的主要内容,如果未能解决你的问题,请参考以下文章