题解 | 从单向链表中删除指定值的节点
从单向链表中删除指定值的节点
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")#转行#