题解 | 从单向链表中删除指定值的节点
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <iostream>
using namespace std;
#include<unordered_map>
int main() {
int n,h,k;
cin>>n>>h;
unordered_map<int , int>mp;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
if(mp.find(b)==mp.end()){
mp.insert(pair(b, a));
}else{
int temp=mp[b];
mp[b]=a;
mp.insert(pair(a,temp));
}
}
cin>>k;
if(h==k){
h=mp[h];
}else{
int it=h;
while(mp[it]){
if(mp[it]==k){
int temp=mp[mp[it]];
mp[it]=temp;
break;
}else{
it=mp[it];
}
}
}
cout<<h<<' ';
for(int i=0;i<n-2;i++){
cout<<mp[h]<<' ';
h=mp[h];
}
}
// 64 位输出请用 printf("%lld")
安克创新 Anker公司福利 896人发布