1097 Deduplication on a Linked List (25)

Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept. At the mean time, all the removed nodes must be kept in a separate list. For example, given L being 21→-15→-15→-7→15, you must output 21→-15→-7, and the removed list -15→15.

Input Specification:

Each input file contains one test case. For each case, the first line contains the address of the first node, and a positive N (<= 10^5^) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.

Then N lines follow, each describes a node in the format:

Address Key Next

where Address is the position of the node, Key is an integer of which absolute value is no more than 10^4^, and Next is the position of the next node.

Output Specification:

For each case, output the resulting linked list first, then the removed list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:

00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854

Sample Output:

00100 21 23854
23854 -15 99999
99999 -7 -1
00000 -15 87654
87654 15 -1

题目大意:给出一些节点信息; 要求把具有不同绝对值的节点组合成一条链表, 绝对值和之前相同的结点组合成另外一条链表
思路:用vector<int> v来保存当前节点指向下一节点的指针, vector<int> value 保存当前节点的值, vector<int> exist保存数值的出现情况; 从头结点开始遍历,根据是否重复把节点添加到不同的链表中; 输出的时候,更改节点的指针

复制代码
 1 #include<iostream>
 2 #include<vector>
 3 #include<math.h>
 4 using namespace std;
 5 struct node{
 6   int addr, val, next;
 7 };
 8 int main(){
 9   int root, n, i;
10   cin>>root>>n;
11   vector<int>  v(100000, -1), value(100000);
12   int addr, val, next, r;
13   bool flag=true;
14   for(i=0; i<n; i++){
15     scanf("%d %d %d", &addr, &val, &next);
16     v[addr]=next;
17     value[addr]=val;
18   }
19   vector<node> l1, l2;
20   node nnode;
21   vector<int> exist(10010, -1);
22   while(true){
23     if(root==-1) break;
24     nnode.addr=root; nnode.val=value[root]; 
25     int temp=abs(value[root]);
26     if(exist[temp]==-1){l1.push_back(nnode); exist[temp]=1;}
27     else l2.push_back(nnode);
28     root=v[root];
29   }
30   for(i=0; i<l1.size(); i++){
31     if(i!=l1.size()-1) printf("%05d %d %05d\n", l1[i].addr, l1[i].val, l1[i+1].addr);
32     else printf("%05d %d %d\n", l1[l1.size()-1].addr, l1[l1.size()-1].val, -1);
33   }
34   for(i=0; i<l2.size(); i++){
35     if(i!=l2.size()-1) printf("%05d %d %05d\n", l2[i].addr, l2[i].val, l2[i+1].addr);
36     else printf("%05d %d %d\n", l2[l2.size()-1].addr, l2[l2.size()-1].val, -1);
37   }
38   return 0;
39 }
复制代码

pat中链表类的题目大多都是这种模式,使用的还不是很熟练还需要多理解

posted @   赖兴宇  阅读(121)  评论(0编辑  收藏  举报
编辑推荐:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
阅读排行:
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!
点击右上角即可分享
微信分享提示