-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate-list.cc
34 lines (31 loc) · 925 Bytes
/
rotate-list.cc
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
#include "leetcode.h"
using namespace std;
using namespace utils;
class Solution {
public:
ListNode *rotateRight(ListNode *head, int k) {
if (head == nullptr) return nullptr;
ListNode *tail = nullptr;
auto count = 1;
for (tail = head; tail->next != nullptr; tail = tail->next) ++count;
tail->next = head;
for (auto i = 0; i < count - k % count; ++i) {
head = head->next;
tail = tail->next;
}
tail->next = nullptr;
return head;
};
};
int main(int argc, char const *argv[]) {
Solution solution;
const vector<int> vec{0};
const auto node = make_nodes(vec);
foreach (node, [](const int v) { cout << v << " "; })
;
cout << endl;
const auto res = solution.rotateRight(node, 4);
foreach (res, [](const int v) { cout << v << " "; })
;
del_nodes(node);
}