题解 | 从单向链表中删除指定值的节点

从单向链表中删除指定值的节点

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")

全部评论

相关推荐

零零幺零零幺:至少再做一个项目,然后猛投小厂,不然有点难
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务