-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path61. Rotate List.cpp
42 lines (39 loc) · 1005 Bytes
/
61. Rotate List.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/**
* 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;
int len=getLength(head);
ListNode *last=head;
while(last->next!=NULL)//it should be last->next
last=last->next;
k=k%len;
if(k==0)
return head;
ListNode *tmp=head;
for(int i=1;i<len-k;i++)
tmp=tmp->next;//the one before k(th)
last->next=head;
head=tmp->next;
tmp->next=NULL;
return head;
}
int getLength(ListNode *head){
if(head==NULL)
return 0;
else
return 1+getLength(head->next);
}
};
//链表的题目还有很大问题
//依然存在小部分问题
// k=0那边没有考虑啊
//不会了,写的代码错误百出啊