L2-022 重排链表
离谱,给的节点中可能还有多余的节点,谜语人
#include <bits/stdc++.h>
using namespace std;
#define val first
#define nex second
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int init, n;
cin >> init >> n;
map<int, pair<int, int>> link;
for (int i = 0; i < n; i++) {
int ad, data, nxt;
cin >> ad >> data >> nxt;
link[ad] = {data, nxt};
}
vector<pair<int, int>> num;
int var = init;
while (true) {
num.push_back({var, link[var].val});
if (link[var].nex == -1) break;
var = link[var].nex;
}
int i = 0, j = (int)num.size() - 1;
vector<pair<int, int>> res;
while (i <= j) {
res.push_back(num[j]); j--;
if (i > j) break;
res.push_back(num[i]); i++;
}
for (int i = 0; i < res.size(); i++) {
if (i == res.size() - 1) {
printf("%05d %d %d", res[i].first, res[i].second, -1);
//cout << res[i].first << " " << res[i].second << " -1";
} else {
printf("%05d %d %05d\n", res[i].first, res[i].second, res[i + 1].first);
//cout << res[i].first << " " << res[i].second << " " << res[i + 1].first << "\n";
}
}
return 0;
}