当前位置:网站首页>Group programming ladder race - exercise set l2-002 linked list de duplication
Group programming ladder race - exercise set l2-002 linked list de duplication
2022-07-04 07:59:00 【Cod_ ing】
characteristic : use Hash surface
#include<iostream>
#include<unordered_map>
#include<map>
#include<string>
#include<vector>
using namespace std;
struct ListNode {
string addr;
string next;
int data;
};
int f_abs(int x) {
if (x > 0) return x;
return -x;
}
int main() {
unordered_map<string, ListNode> LinkList; //Hash surface , Store the original linked list
vector<ListNode> Deleted; // Store the deleted linked list nodes
map<int, int> nums; // Mark whether this number exists
string head;
int n;
cin >> head >> n;
string addr, next;
int data;
int cur = 0;
for (int i = 0; i < n; i++) {
cin >> addr >> data >> next;
LinkList[addr].addr = addr;
LinkList[addr].data = data;
LinkList[addr].next = next;
}
LinkList["-1"]; //address=-1 As the termination condition of iteration
string pre_node = head;
string node = LinkList[head].next;
nums[f_abs(LinkList[pre_node].data)] = 1; // First store the number of header nodes
while (node!= "-1") {
int x = f_abs(LinkList[node].data);
nums[x]++;
if (nums[x]>1) {
// This number has appeared before
if (cur > 0) {
// The previous node of the node in the deleted node linked list needs special treatment , Change next value
Deleted[cur - 1].next = node;
}
Deleted.push_back({
node, "-1", LinkList[node].data }); // Inserted nodes next The assignment is -1
cur++;
LinkList[pre_node].next = LinkList[node].next;
node = LinkList[node].next;
}
else {
pre_node = node;
node = LinkList[node].next;
}
}
//cout << endl;
node = head;
while (node != "-1") {
cout << node << " " << LinkList[node].data << " " << LinkList[node].next<<endl;
node = LinkList[node].next;
}
//cout << endl; Output delete node
for (ListNode x : Deleted) {
cout << x.addr << " " << x.data << " " << x.next << endl;
}
return 0;
}
边栏推荐
- Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
- 运动【跑步 01】一个程序员的半马挑战:跑前准备+跑中调整+跑后恢复(经验分享)
- MySQL中的文本處理函數整理,收藏速查
- Jianmu continuous integration platform v2.2.2 release
- University stage summary
- L1-024 the day after tomorrow (5 points)
- JVM -- class loading process and runtime data area
- How to set multiple selecteditems on a list box- c#
- [gurobi] establishment of simple model
- Heap concept in JVM
猜你喜欢
[gurobi] establishment of simple model
墨者学院-Webmin未经身份验证的远程代码执行
In the era of low code development, is it still needed?
1. Getting started with QT
Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
深入浅出:了解时序数据库 InfluxDB
Blog stop statement
Text processing function sorting in mysql, quick search of collection
BUUCTF(3)
PCIE知识点-010:PCIE 热插拔资料从哪获取
随机推荐
Activiti常见操作数据表关系
OKR vs. KPI 一次搞清楚这两大概念!
Laravel page load problem connection reset - PHP
Moher College webmin unauthenticated remote code execution
R language ggplot2 visualization: ggplot2 visualization grouping box diagram, place the legend and title of the visualization image on the top left of the image and align them to the left, in which th
Write a thread pool by hand, and take you to learn the implementation principle of ThreadPoolExecutor thread pool
In the era of low code development, is it still needed?
Ecole bio rushes to the scientific innovation board: the annual revenue is 330million. Honghui fund and Temasek are shareholders
SQL注入测试工具之Sqli-labs下载安装重置数据库报错解决办法之一(#0{main}thrown in D:\Software\phpstudy_pro\WWW\sqli-labs-……)
墨者学院-phpMyAdmin后台文件包含分析溯源
Go h*ck yourself:online reconnaissance (online reconnaissance)
墨者学院-PHPMailer远程命令执行漏洞溯源
Leetcode 23. 合并K个升序链表
System architecture design of circle of friends
Types of references in BibTex
L1-027 rental (20 points)
NPM run build error
Go learning notes - constants
Leetcode 23. Merge K ascending linked lists
yolov5 xml数据集转换为VOC数据集