题解 | #从单向链表中删除指定值的节点# 自定义结构体实现
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <iostream>
using namespace std;
struct list_a {
int node = 0;
list_a* next = nullptr;
list_a(int value, list_a* ne) : node(value), next(ne) {};
list_a(int value) : node(value) {};
list_a(list_a* ne) : next(ne) {};
};
int main() {
int n, he;
cin >> n;
cin >> he;
auto head = new list_a(he);
for (int i = 0; i < n - 1; i++) {
int a, b;
cin >> a >> b;
list_a* tmp = head;
// 寻找插入的节点
while (tmp->node != b) {
tmp = tmp->next;
}
// 进行插入操作
list_a* tmp1 = tmp->next;
tmp->next = new list_a(a);
tmp->next->next = tmp1;
}
int tar;
cin >> tar;
list_a* tmp3 = head, *pre = head;
if(head->node == tar){
list_a* tmp = head;
head = head->next;
}else{
while (tmp3->node != tar) {
pre = tmp3;
tmp3 = tmp3->next;
}
}
list_a* tm = tmp3;
pre->next = pre->next->next;
delete tm;
while(head != nullptr){
cout << head->node << ' ';
list_a* tmp = head;
head = head->next;
delete tmp;
}
return 0;
}
// 64 位输出请用 printf("%lld")


查看26道真题和解析