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

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

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n, h;
    cin >> n >> h;

    vector<int> order;
    order.push_back(h);
    for(int i = 0; i < n-1; ++i) {
        int a, b;
        cin >> a >> b;

        auto it = find(order.begin(), order.end(), b);
        order.insert(it + 1, a); //insert是在迭代器前面插入 所以+1
    }

    int k;
    cin >> k;
    for(const int& n: order) {
        if(n != k) {
            cout << n << " ";
        }
    }
    return 0;
}

全部评论

相关推荐

03-31 17:43
重庆大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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