網(wǎng)站建設(shè)編程語言常用的seo查詢工具有哪些
題目鏈接
描述
輸入一個(gè)單向鏈表和一個(gè)節(jié)點(diǎn)的值,從單向鏈表中刪除等于該值的節(jié)點(diǎn),刪除后如果鏈表中無節(jié)點(diǎn)則返回空指針。
鏈表的值不能重復(fù)。
構(gòu)造過程,例如輸入一行數(shù)據(jù)為:
6 2 1 2 3 2 5 1 4 5 7 2 2
則第一個(gè)參數(shù)6表示輸入總共6個(gè)節(jié)點(diǎn),第二個(gè)參數(shù)2表示頭節(jié)點(diǎn)值為2,剩下的2個(gè)一組表示第2個(gè)節(jié)點(diǎn)值后面插入第1個(gè)節(jié)點(diǎn)值,為以下表示:
1 2 表示為
2->1
鏈表為2->1
3 2表示為
2->3
鏈表為2->3->1
5 1表示為
1->5
鏈表為2->3->1->5
4 5表示為
5->4
鏈表為2->3->1->5->4
7 2表示為
2->7
鏈表為2->7->3->1->5->4
最后的鏈表的順序?yàn)?2 7 3 1 5 4
最后一個(gè)參數(shù)為2,表示要?jiǎng)h掉節(jié)點(diǎn)為2的值
刪除 結(jié)點(diǎn) 2
則結(jié)果為 7 3 1 5 4
數(shù)據(jù)范圍:鏈表長度滿足 1≤n≤1000 ,節(jié)點(diǎn)中的值滿足 0≤val≤10000
測試用例保證輸入合法
輸入描述:
輸入一行,有以下4個(gè)部分:
1 輸入鏈表結(jié)點(diǎn)個(gè)數(shù)
2 輸入頭結(jié)點(diǎn)的值
3 按照格式插入各個(gè)結(jié)點(diǎn)
4 輸入要?jiǎng)h除的結(jié)點(diǎn)的值
輸出描述:
輸出一行
輸出刪除結(jié)點(diǎn)后的序列,每個(gè)數(shù)后都要加空格
示例1
輸入:
5 2 3 2 4 3 5 2 1 4 3
輸出:
2 5 4 1
說明:
形成的鏈表為2->5->3->4->1
刪掉節(jié)點(diǎn)3,返回的就是2->5->4->1
示例2
輸入:
6 2 1 2 3 2 5 1 4 5 7 2 2
輸出:
7 3 1 5 4
solution:常規(guī)鏈表操作,套用STL即可
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;int main() {int n, head, x, y;cin >> n >> head;n--;list<int> lst;lst.push_back(head);while (n--){cin >> x >> y;for (auto it(lst.begin()); it != lst.end(); ++it){if (*it == y){lst.insert(++it, x);break;}}}cin >> n;for (auto it(lst.begin()); it !=lst.end(); ++it) {if (*it == n){lst.erase(it);break;}}for (auto it(lst.begin()); it !=lst.end(); ++it) {cout << *it << ' ';}return 0;
}
// 64 位輸出請(qǐng)用 printf("%lld")