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. 【模板篇】A* 寻路算法

    上次在做k短路的时候说到了A*, 但是并没有仔细的研究A*寻路, 毕竟k短路中的A*也不怎么标准… A*寻路的过程网上还是有很多的, 讲得也很清楚, 不妨跟着里面的图示自己动手操作一下, 基本一遍就能 ...

  2. 前端常用的库和实用技术之JavaScript高级函数

    1.惰性载入函数 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. String str = new String("abc"),这段代码一共生成了几个String对象?为什么?

    String str = new String("abc")创建了俩个对象,首先为创建一个String对象"abc",然后在调用String类的构造方法时 pu ...

  4. Maven - Scope区别

    依赖的Scope scope定义了类包在项目的使用阶段.项目阶段包括: 编译,运行,测试和发布. 分类说明 compile 默认scope为compile,表示为当前依赖参与项目的编译.测试和运行阶段 ...

  5. CSS——背景渐变

    在线性渐变过程中,颜色沿着一条直线过渡:从左侧到右侧.从右侧到左侧.从顶部到底部.从底部到顶部或着沿任何任意轴.如果你曾使用过制作图件,比如说Photoshop,你对线性渐变并不会陌生. 兼容性问题很 ...

  6. 计算几何——直线交点poj1269

    求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...

  7. Java 对系统信号的通知获取

    主要介绍Java 如何对系统信号通知进行获取和处理.直接上demo @SuppressWarnings("restriction")public class Test1 imple ...

  8. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  9. 杂项-语言-Swift:Swift

    ylbtech-杂项-语言-Swift:Swift Swift,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于 ...

  10. JSF(JavaServer Faces)简介

    JavaServer Faces (JSF) 是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准).它提供了一种以组件为中心的 ...