-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotate_List.cpp
More file actions
39 lines (38 loc) · 758 Bytes
/
Rotate_List.cpp
File metadata and controls
39 lines (38 loc) · 758 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
ListNode* listlength(ListNode* tmp){
int cnt = 0;
ListNode* prev = NULL;
while(tmp){
cnt++;
prev = tmp;
tmp = tmp->next;
}
ListNode* t = new ListNode(cnt);
t->next = prev;
return t;
}
ListNode* Solution::rotateRight(ListNode* A, int B) {
ListNode* tmp = listlength(A);
int n = tmp->val;
B %= n;
if (B==0)
return A;
ListNode* last = tmp->next;
last->next = A;
int i = n-B;
ListNode* prev = NULL;
while(i){
i--;
prev = A;
A = A->next;
}
prev->next = NULL;
return A;
}