当前位置:网站首页>【PTA】L2-002 Deduplication of linked list (25 points)
【PTA】L2-002 Deduplication of linked list (25 points)
2022-08-07 14:41:00 【JKL27】
题目链接
给定一个带整数键值的链表 L,你需要把其中绝对值重复的键值结点删掉.即对每个键值 K,只有第一个绝对值等于 K 的结点被保留.同时,所有被删除的结点须被保存在另一个链表上.例如给定 L 为 21→-15→-15→-7→15,你需要输出去重后的链表 21→-15→-7,还有被删除的链表 -15→15.
输入格式
输入在第一行给出 L 的第一个结点的地址和一个正整数 N(≤ 1 0 5 10^5 105,为结点总数).一个结点的地址是非负的 5 位整数,空地址 NULL 用 −1 来表示.
随后 N 行,每行按以下格式描述一个结点:
地址 键值 下一个结点
其中地址是该结点的地址,键值是绝对值不超过 1 0 4 10^4 104的整数,下一个结点是下个结点的地址.
输出格式
首先输出去重后的链表,然后输出被删除的链表.每个结点占一行,按输入的格式输出.
输入样例:
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
输出样例:
00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1
实现思路
Mainly deal with absolute value of repeated point,设置 delPre To deal with the deleted node in the list.
我用了map,,,但感觉set更适合一点.
When the output is to delete the list of before,第三个 %d 少了 05,The result didn't just read out T-T
实现代码(C++)
#include <iostream>
#include <iomanip>
#include <map>
using namespace std;
int main() {
map<int,pair<int,int>> list, del; // curr, <value, next>
int head, n;
scanf("%d %d", &head, &n);
// head node
list[head] = make_pair(-1, -1);
for (int i = 0; i < n; i++) {
int curr, val, next;
scanf("%d %d %d", &curr, &val, &next);
list[curr] = make_pair(val, next);
}
map<int,int> rVal; // map for reserve val
int temp = head, pre, delPre = -1;
int delHead = -1;
while (temp != -1) {
rVal[abs(list[temp].first)]++;
if (rVal[abs(list[temp].first)] <= 1) {
pre = temp;
} else {
list[pre].second = list[temp].second;
if (delHead == -1) {
delHead = temp;
} else {
del[delPre].second = temp;
}
del[temp] = make_pair(list[temp].first, -1);
delPre = temp;
}
temp = list[temp].second;
}
temp = head;
while (temp != -1) {
int next = list[temp].second;
if (next != -1) {
printf("%05d %d %05d\n", temp, list[temp].first, list[temp].second);
} else {
printf("%05d %d -1\n", temp, list[temp].first);
}
temp = list[temp].second;
}
temp = delHead;
while (temp != -1) {
int next = del[temp].second;
if (next != -1) {
printf("%05d %d %05d\n", temp, del[temp].first, del[temp].second); // 就是在这里
} else {
printf("%05d %d -1\n", temp, del[temp].first);
}
temp = del[temp].second;
}
return 0;
}
边栏推荐
猜你喜欢

Codeforces Round #812 (Div. 2)

【数据库系统原理】第四章 高级数据库模型:E/R模型及其设计规则、约束

Lianshengde W801 series 4-MQTT use

2022牛客多校六 A-Array(构造+哈夫曼)

LeetCode 热题 HOT 100(9.电话号码的字母组合)

联盛德W801系列1-flash保存数据例程:保存wifi配网信息

GCB | 华中农大刘玉荣组在海拔梯度下土壤碳周转领域取得系列新进展

俩日总结(【18】、【19】)

C专家编程 第8章 为什么程序员无法分清万圣节和圣诞节 8.5 原型在什么地方会失败

Lianshengde W801 series 2-WIFI one-key distribution network, information preservation
随机推荐
Postgresql logical backup tool pg_dump and pg_resotre learning
d浮点小问题
GCB | 华中农大刘玉荣组在海拔梯度下土壤碳周转领域取得系列新进展
1403. 非递增顺序的最小子序列
使用同花顺软件炒股安全吗?
云信小课堂 | 基于 NERoom 快速实现在线会议
Expert C Programming Chapter 8 Why Programmers Can't Tell the Difference Between Halloween and Christmas 8.2 Building Graphics from Bit Patterns
嵌入式开发:嵌入式基础–了解微控制器引导过程
多线程-同步问题
dotnet 特性 DynamicallyInvokable 是用来做什么的
Acwing/3359. 更多奇怪的照片
Summary of the open surface
多线程-线程的状态、优先级、守护进程
Hash table | 1. The sum of two numbers, 454. The addition of four numbers | The most suitable `dictionary key-value` | leecode brush the notes
HJ3 obvious random number
LeetCode 热题 HOT 100(10.删除链表的倒数第 N 个结点)
dotnet 数组自动转基类数组提示 Co-variant array conversion 是什么问题
C专家编程 第8章 为什么程序员无法分清万圣节和圣诞节 8.3 在等待时类型发生了变化
做一个物联网云平台到底要多少钱?
dotnet 通过 WMI 获取系统信息