题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
int head;
cin >> n >> head;
forward_list<int> flist;
flist.push_front(head);
for (int i = 1; i < n ; i++) {
int front, back;
cin >> back >> front;
// cout << "cin back:" << back << endl;
auto iter = find(flist.begin(), flist.end(), front);
// cout << "back:" << back << endl;
flist.insert_after(iter, back);
}
int tor;
cin >> tor;
// cout << "back:" << tor << endl;
flist.remove(tor);
for (auto ch : flist) {
cout << ch << ' ' ;
}
}

查看1道真题和解析