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

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

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

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n, h;
    while (cin >> n >> h) {
        forward_list<int> linklist; //创建单链表linklist
        linklist.push_front(h);
        int front, rear;
        for (int i = 1; i < n ; ++i) {
            cin >> rear >> front;
            auto iter = find(linklist.begin(), linklist.end(),
                           front);  //寻找front的位置
            linklist.insert_after(iter, rear); //在front所在位置后插入rear
        }
        int to_del;
        cin >> to_del;
        linklist.remove(to_del); //删除该值的节点
        for (int& it : linklist) {
            cout << it << " "; //从头到尾输出链表的值
        }
    }
    return 0;
}

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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