题解 | #从单向链表中删除指定值的节点#
从单向链表中删除指定值的节点
https://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f
#include <cmath> #include <iostream> #include<map> #include <sstream> using namespace std; int main() { map<int, int> list; string s; int behind = 0; int before = 0; int num = 0; int head = 0; int tem; int to_delete = 0; cin >> num; cin >> head; list.insert({head, -1}); getline(cin, s); int digit = 0; for(int i = s.size()-1;i>=0;i--){ if(s[i]>='0'&&s[i]<='9'){ to_delete+=(s[i]-'0')*pow(10,digit); digit++; }else{ break; } } s.erase(s.size() - 1, 1); istringstream ss(s); while (ss >>behind>>before) { tem = list[before]; list[before] = behind; list.insert({behind, tem}); } int i = head; while (i != -1) { if (list[i] == to_delete) { tem = list[i]; list[i] = list[tem]; list.erase(tem); break; } i = list[i]; } i = head; while(i!=-1){ cout<<i<<' '; i = list[i]; } } // 64 位输出请用 printf("%lld")
哈希表实现链表,便于操作,单词插入删除时间复杂度为n