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

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

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

#include <iostream>
using namespace std;
#include <list>
int main()
{
    int num;
    cin >> num;
    list<int> lst;
    int head;
    cin >> head;
    lst.push_front(head);
    int p; // 前节点
    int n; // 后节点
    for (int i = 1; i < num; i++)
    {
        cin >> n; // 第一个参数为后节点
        cin >> p; // 第二个参数为前节点
        for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
        {
            if (*it == p)
            {
                lst.insert(it, n);
            }
        }
    }
    int d;
    cin >> d;
    lst.remove(d);
    lst.reverse();
    for (list<int>::iterator it = lst.begin(); it != lst.end(); it++)
    {
        cout << *it << " ";
    }
}

C++ STL list的应用,这里插反了问题不大用个翻转完事()()()

华为机试刷题记录 文章被收录于专栏

记录一下手打代码的解题思路方便复习

全部评论

相关推荐

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

创作者周榜

更多
牛客网
牛客企业服务