-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path61.cpp
More file actions
28 lines (28 loc) · 681 Bytes
/
Copy path61.cpp
File metadata and controls
28 lines (28 loc) · 681 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
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if (!head || !head->next || k == 0)
return head;
int n = 0;
ListNode *temp = head, *tail = nullptr, *start = head, *end = nullptr;
while (temp) {
if (!temp->next)
tail = temp;
temp = temp->next;
++n;
}
k %= n;
if(k == 0)
return head;
n -= k;
while (n > 0) {
if (n == 1)
end = start;
start = start->next;
--n;
}
tail->next = head;
end->next = nullptr;
return start;
}
};