题目链接:
L2-002 链表去重
思路:
将两个链表分开存储、输出即可;
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
bool vst[maxn];
int nex[maxn], val[maxn];
void print(vector<int> & v){
if(!v.size()) return;
printf("%05d %d ", v[0], val[v[0]]);
for(int i = 1; i < v.size(); i++){
printf("%05d
%05d %d ",v[i], v[i], val[v[i]]);
}
printf("-1
");
}
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
int s, n;
scanf("%d %d", &s, &n);
for(int i = 0; i < n; i++) {
int p;
scanf("%d", &p);
scanf("%d %d", val + p, nex + p);
}
vector<int> x, y;
while(~s) {
if(!vst[abs(val[s])]){
vst[abs(val[s])] = true;
x.push_back(s);
}else y.push_back(s);
s = nex[s];
}
print(x);
print(y);
return 0;
}