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 (≤) which is the total number of nodes. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −.

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 1, 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
 #include <iostream>
#include <unordered_map>
#include <cmath>
using namespace std;
struct Node
{
int addr, val;
Node(int a, int b) :addr(a), val(b) {}
};
struct List
{
Node val;
List* next;
List(Node a) :val(a), next(nullptr) {}
};
int N, head, addr1, addr2, val, numbers[] = { };
int main()
{
cin >> head >> N;
if (head == -)//切记,此时有一个测试例子是头结点为-1,那么什么也不输出
return ;
unordered_map<int, pair<int, int>>map;
for (int i = ; i < N; ++i)
{
cin >> addr1 >> val >> addr2;
map[addr1] = make_pair(val, addr2);
}
//将链表组合起来
List* resHead = new List(Node(, -));
List* delHead = new List(Node(, -));
List* p = resHead;
while (head != -)
{
List* q = new List(Node(head, map[head].first));
p->next = q;
p = q;
head = map[head].second;
}
List* pre = resHead, *delP = delHead;
p = pre->next;
while (p != nullptr)
{
if (numbers[abs(p->val.val)] == )
{
pre->next = p->next;//删除
List* q = new List(Node(p->val.addr, p->val.val));
delP->next = q;
delP = q;
delete p;
p = pre->next;
}
else
{
numbers[abs(p->val.val)]++;
pre = p;
p = pre->next;
}
}
p = resHead->next;
while (p != nullptr)
{
printf("%05d %d ", p->val.addr, p->val.val);
p = p->next;
if (p == nullptr)
printf("%d\n", -);
else
printf("%05d\n", p->val.addr);
}
p = delHead->next;
while (p != nullptr)
{
printf("%05d %d ", p->val.addr, p->val.val);
p = p->next;
if (p == nullptr)
printf("%d\n", -);
else
printf("%05d\n", p->val.addr);
}
return ;
}

PAT甲级——A1097 Deduplication on a Linked List的更多相关文章

  1. PAT甲级——1097 Deduplication on a Linked List (链表)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982 1097 Deduplication on a L ...

  2. PAT A1097 Deduplication on a Linked List (25 分)——链表

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  3. A1097. Deduplication on a Linked List

    Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated ...

  4. PAT Advanced 1097 Deduplication on a Linked List (25) [链表]

    题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplica ...

  5. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  6. PAT_A1097#Deduplication on a Linked List

    Source: PAT A1097 Deduplication on a Linked List (25 分) Description: Given a singly linked list L wi ...

  7. PAT 1097 Deduplication on a Linked List[比较]

    1097 Deduplication on a Linked List(25 分) Given a singly linked list L with integer keys, you are su ...

  8. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  9. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. 【csp】2017-12

    第一题:游戏 题目: 题意:啊,不多赘述.看的懂. 题解:sort一下直接暴力比较大小. 代码: #include<iostream> #include<cstdio> #in ...

  2. 初探.Net Core API 网关Ocelot(一)

    一.介绍 Ocelot 是基于.NetCore实现的开源的API网关,支持IdentityServer认证.Ocelot具有路由.请求聚合.服务发现.认证.鉴权.限流熔断等功能,并内置了负载均衡器与S ...

  3. bzoj 3579: 破冰派对

    题意: 给你一个图,问你有多少个方案把他分成连个新的图.使得一个图是一个团,另外一个是独立集 一些闲话: 以前做过一次这个题..当时听说爆搜可以过,就无脑莽过去了.. 也没有思考为什么爆搜能过,或者有 ...

  4. MySQL sql_mode 说明(及处理一起sql_mode引发的问题)

    转自:https://segmentfault.com/a/1190000005936172 1. MySQL 莫名变成了 Strict SQL Mode 最近测试组那边反应数据库部分写入失败,app ...

  5. 解决vs code 内置终端,字体间隔过大问题。(linux centos7 ubuntu成功)

    去文件-首选项-设置里修改. "terminal.integrated.fontFamily": ""注意此处默认为空白,所以显示的就比较奇怪. 此处我改为&q ...

  6. Android开发 LiveData与MutableLiveData详解

    前言 LiveData与ViewMode是经常搭配在一起使用的,但是为了不太混乱,我还是拆分开来说明,此篇博客只讲解 LiveData 与 MutableLiveData的概念与使用方式(但是会涉及到 ...

  7. ES5-call,apply,bind的用法

    区别bind()与call()和apply()? 1. Function.prototype.bind(obj) : * 作用: 将函数内的this绑定为obj, 并将函数返回2. 面试题: 区别bi ...

  8. leetcode-47-全排列②

    题目描述: 方法一:回溯 class Solution: def permuteUnique(self, nums: List[int]) -> List[List[int]]: #nums = ...

  9. python 之单例模式

    单例模式1 单例=>只有一个单例2 静态方法+静态字段3 所有实例中等转的内容相同时 用单例模式class Sqllite: __instance=None def __init__(self) ...

  10. git 大型灾难现场

    由于某种原因,需要重建git仓库.因此删了所有分支,重建git仓库. 删除整个过程 删除所有分支(除master外) git branch -d {branch_name}  # 删除本地分支 git ...