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

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

https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f?tpId=37&tqId=21271&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37&difficulty=3&judgeStatus=undefined&tags=&title=

#include <iostream>
using namespace std;
struct ListNode {
    int val;
    struct ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};
int main() {
    int n, h, a, b;

    cin >> n;
    auto* head = new ListNode(0), *ph = new ListNode(0);
    cin >> h;
    head->val = h;
    head->next = nullptr;
    n--;
    while (n--) {
        cin >> a >> b;
        for (auto ph = head; ph != nullptr; ph = ph->next) {
            if (b == ph->val) {
                auto* nd = new ListNode(a);
                nd->next = ph->next;
                ph->next = nd;
            }
        }
    }
    int del;
    cin >> del;
    for (auto cur = head; cur != nullptr; cur = cur->next) {
        if (cur->val != del)cout << cur->val << ' ';
    }
}
// 64 位输出请用 printf("%lld")

#转行#
全部评论

相关推荐

点赞 评论 收藏
分享
07-21 12:41
已编辑
门头沟学院 Java
steelhead:不是你的问题,这是社会的问题。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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