题解 | 从单向链表中删除指定值的节点
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <iostream>
using namespace std;
struct node {
int val;
struct node *next = NULL;
};
int main() {
int n,val;
while(cin >> n >>val){
node * head = new node;
head->val = val;
for(int i = 1 ; i < n ; i++){
int pre,cur;
cin >> cur >> pre;
node *p = new node;
p->val = cur;
node *q = head;
while(q->val != pre){
q = q->next;
}
p->next = q->next;
q->next = p;
}
int remove;
cin >> remove ;
node *p = head;
while(p != nullptr){
if(p->val != remove){
cout<< p->val << " ";
}
p =p->next;
}
cout << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
很多细节需要注意。
