当前位置:网站首页>ACM. HJ48 从单向链表中删除指定值的节点 ●●
ACM. HJ48 从单向链表中删除指定值的节点 ●●
2022-06-30 07:23:00 【chenyfan_】
HJ48 从单向链表中删除指定值的节点 ●●
描述
输入一个单向链表和一个节点的值,从单向链表中删除等于该值的节点,删除后如果链表中无节点则返回空指针。
链表的值不能重复。
构造过程,例如输入一行数据为:
6 2 1 2 3 2 5 1 4 5 7 2 2
则第一个参数6表示输入总共6个节点,第二个参数2表示头节点值为2,剩下的2个一组表示第2个节点值后面插入第1个节点值,为以下表示:
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
最后的链表的顺序为 2 7 3 1 5 4
最后一个参数为2,表示要删掉节点为2的值
删除 结点 2
则结果为 7 3 1 5 4
数据范围:链表长度满足 1 ≤ n ≤ 1000 1 \le n \le 1000 1≤n≤1000 ,节点中的值满足 0 ≤ v a l ≤ 10000 0 \le val \le 10000 0≤val≤10000
测试用例保证输入合法
输入描述:
输入一行,有以下4个部分:
1 输入链表结点个数
2 输入头结点的值
3 按照格式插入各个结点
4 输入要删除的结点的值
输出描述:
输出一行
输出删除结点后的序列,每个数后都要加空格
示例
输入:
5 2 3 2 4 3 5 2 1 4 3
输出:
2 5 4 1
说明:
形成的链表为2->5->3->4->1
删掉节点3,返回的就是2->5->4->1
题解
1. 链表 插入 删除
构建链表,然后依次将结点插入。
插入的时候遍历链表找到插入结点的前序结点(因为表中元素互异),断开前序结点与后序结点的连接,插入结点连接后续结点,前序结点连接插入结点。
后续直接遍历链表输出,当遇到要删除的结点值时不输出即可。
- 时间复杂度: O ( n 2 ) O(n^2) O(n2),插入的时候遍历 n−1 个输入,每个输入都要遍历链表找到插入位置
- 空间复杂度: O ( 1 ) O(1) O(1),链表空间属于必要空间,不属于额外空间

#include <iostream>
#include <vector>
using namespace std;
struct ListNode{
int val;
ListNode* next;
ListNode(int v) : val(v), next(nullptr){
}
ListNode(){
}
};
int cnt = 0;
void insert(ListNode* dummyHead, int val, int pre_v){
ListNode* pre = dummyHead;
ListNode* curr = pre->next; // head
for(int i = 1; i <= cnt; ++i){
if(curr->val == pre_v){
// 找到插入的位置
pre = curr;
curr = curr->next;
break;
}
curr = curr->next;
}
ListNode* node = new ListNode(val);
pre->next = node;
node->next = curr;
}
int main(){
int n;
int head_val;
while(cin >> n >> head_val){
ListNode* dummyHead = new ListNode(-1);
ListNode* head = new ListNode(head_val);
dummyHead->next = head;
cnt = 1;
for(int i = 0; i < n-1; ++i){
int v, pre_v;
cin >> v >> pre_v;
insert(dummyHead, v, pre_v);
++cnt;
// ListNode* cur = dummyHead->next; // 输出链表
// for(int j = 0; j <= i+1; ++j){
// cout << cur->val << "->";
// cur = cur->next;
// }
// cout << endl;
}
int del;
cin >> del;
ListNode* pre = dummyHead;
ListNode* curr = dummyHead->next;
for(int i = 0; i < n; ++i){
if(curr->val != del){
cout << curr->val << " ";
}else{
pre->next = curr->next; // 删除指定节点
delete curr;
curr = pre->next;
continue;
}
pre = curr;
curr = curr->next;
}
}
return 0;
}
2. 数组模拟
因为链表中的值互异,我们可以用数组来模拟链表进行增删。
首先数组添加第一个元素即表头,然后每次要添加元素我们都要用find函数找到它在哪个数字后面,即找到它前面那个数字的出现的位置,因为数字互异所以一定能找到唯一,如果是最后一个数字则将其加入数组末尾,如果是中间的则用 insert 函数插入相应位置。
- 时间复杂度: O ( n 2 ) O(n^2) O(n2),插入的时候遍历 n-1 个输入,每个输入调用 find 函数都是 O(n),删除和输出复杂度较低,舍去
- 空间复杂度: O ( 1 ) O(1) O(1),数组属于必要空间,无额外空间
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
int n, head;
while(cin >> n >> head){
vector<int> array; // 用数组模拟链表
array.push_back(head);
for(int i = 1; i < n; i++){
int num, pos_num;
cin >> num >> pos_num; // 输入要插入的数和它要插入哪个数字后面
auto iter = find(array.begin(), array.end(), pos_num); // 找到要插入后面你的那个位置
if(iter == array.end()) // 结尾push_back
array.push_back(num);
else //中间insert
array.insert(iter + 1, num);
}
int remove;
cin >> remove;
array.erase(find(array.begin(), array.end(), remove)); // 找到要移除的数字的位置
for(int i = 0; i < array.size(); i++) // 输出
cout << array[i] << " ";
cout << endl;
}
return 0;
}
边栏推荐
- January 23, 2022 [reading notes] - bioinformatics and functional genomics (Chapter 6: multiple sequence alignment)
- Introduction notes to pytorch deep learning (XII) neural network - nonlinear activation
- Final review -php learning notes 7-php and web page interaction
- Installation software operation manual (continuous update)
- 为什么大学毕业了还不知道干什么?
- 2021.11.20 [reading notes] | differential variable splicing events and DTU analysis
- RT thread kernel application development message queue experiment
- Basic operation command
- Assembly learning register
- 期末复习-PHP学习笔记9-PHP会话控制
猜你喜欢
![Arm debug interface (adiv5) analysis (I) introduction and implementation [continuous update]](/img/30/375860665aa1cc761adffc0e782744.jpg)
Arm debug interface (adiv5) analysis (I) introduction and implementation [continuous update]

Use of ecostruxure (2) IEC61499 to establish function blocks

Assembly learning register

25岁,从天坑行业提桶跑路,在经历千辛万苦转行程序员,属于我的春天终于来了

Variable storage unit and pointer

期末复习-PHP学习笔记11-PHP-PDO数据库抽象层.

Final review -php learning notes 6- string processing

Final review -php learning notes 4-php custom functions

期末複習-PHP學習筆記5-PHP數組
![November 22, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5, section 4, hidden Markov model)](/img/0d/77953ffa9f45a5acc16f02bf33293b.jpg)
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5, section 4, hidden Markov model)
随机推荐
Final review -php learning notes 5-php array
Raspberry pie 4B Getting Started Guide
Stepper motor
Introduction notes to pytorch deep learning (10) neural network convolution layer
Final review -php learning notes 11-php-pdo database abstraction layer
Global digital industry strategy and policy observation in 2021 (China Academy of ICT)
LabVIEW program code update is slow
November 19, 2021 [reading notes] a summary of common problems of sneakemake (Part 2)
NMOS model selection
uniapp图片下方加标签标图片
Cmake generate map file
Use of ecostruxure (2) IEC61499 to establish function blocks
Ad\dxp how to solve the problem of not knowing the schematic Library
Account command and account authority
期末複習-PHP學習筆記3-PHP流程控制語句
Minecraft 1.16.5 module development (50) guide book
6月底了,可以开始做准备了,不然这么赚钱的行业就没你的份了
July 30, 2021 [wgs/gwas] - whole genome analysis process (Part I)
Xiashuo think tank: 50 planet updates reported today (including the global architects Summit Series)
2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)