当前位置:网站首页>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;
}
边栏推荐
- C language implementation of chain stack (without leading node)
- Parameter calculation of deep learning convolution neural network
- 期末复习-PHP学习笔记8-mysql数据库
- 想转行,却又不知道干什么?此文写给正在迷茫的你
- ADC basic concepts
- Log service management
- Basic knowledge points
- 期末複習-PHP學習筆記3-PHP流程控制語句
- Use of ecostruxure (2) IEC61499 to establish function blocks
- Final review -php learning notes 7-php and web page interaction
猜你喜欢

Account command and account authority

Final review -php learning notes 3-php process control statement

Sublime text 3 configuring the C language running environment

Mailbox application routine of running wild fire RT thread

期末复习-PHP学习笔记1

期末複習-PHP學習筆記3-PHP流程控制語句

right four steps of SEIF SLAM

Network security and data in 2021: collection of new compliance review articles (215 pages)

Installation software operation manual (continuous update)
![Experiment 1: comprehensive experiment [process on]](/img/19/6c6e18d7e1f042bfd3ee4832b78542.png)
Experiment 1: comprehensive experiment [process on]
随机推荐
Common sorting methods
December 4, 2021 - Introduction to macro genome analysis process tools
为什么大学毕业了还不知道干什么?
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5, section 4, hidden Markov model)
C language implementation of chain stack (without leading node)
Implementation of double linked list in C language
Periodic planning work
STM32 infrared communication 2
STM32 register on LED
Implementation of binary search in C language
Analysys analysis: online audio content consumption market analysis 2022
Quick placement of devices by module in Ad
Analysis of cross clock transmission in tinyriscv
Research Report on search business value in the era of big search in 2022
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Section 5 of Chapter 5 uses a comparison tool similar to blast to quickly search genomic DNA)
November 16, 2021 [reading notes] - macro genome analysis process
Xiashuo think tank: 50 planet updates reported today (including the global architects Summit Series)
Final review -php learning notes 1
Variable storage unit and pointer
Self study notes -- use of 74h573