-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy path1025. 反转链表.cpp
30 lines (30 loc) · 898 Bytes
/
1025. 反转链表.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
#include <bits/stdc++.h>
using namespace std;
using gg = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
vector<array<gg, 2>> input(gg(1e6), {-1, -1});
gg start, ni, ki;
cin >> start >> ni >> ki;
for (gg i = 0; i < ni; ++i) {
gg address, data, nextA;
cin >> address >> data >> nextA;
input[address] = {data, nextA};
}
vector<gg> lst;
while (start != -1) {
lst.push_back(start);
start = input[start][1];
}
for (gg i = ki; i <= lst.size(); i += ki) {
reverse(lst.begin() + i - ki, lst.begin() + i);
}
cout << setfill('0');
for (gg i = 0; i < lst.size() - 1; ++i) {
cout << setw(5) << lst[i] << " " << input[lst[i]][0] << " " << setw(5)
<< lst[i + 1] << "\n";
}
cout << setw(5) << lst.back() << " " << input[lst.back()][0] << " -1\n";
return 0;
}