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

本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/91157982

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 Lbeing 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

题目大意:从头结点开始遍历一个链表,将遇到的节点的key的绝对值标记,若重复,则将此节点放到另一个链表里;举个例子,List  21→-15→-15→-7→15,答案是 21→-15→-7 和 -15→15 两个链表。

思路:链表的节点地址是5位整数,-1表示Null ,用数组存放链表。基础的链表删减操作(其实只要更改相应节点的next地址),用 pre 记录前一个节点的地址,addr 记录当前节点的地址,数组也好、set也好、map也好,设置一个容器用于标记已经出现过的abs( key )。

 #include <iostream>
#include <unordered_map>
#include <vector>
#include <cmath>
#define MaxNum 100001
using namespace std; struct node {
int key, next = -;
}; vector <node> List(MaxNum);
unordered_map <int, bool> S; int main()
{
int head, N;
scanf("%d%d", &head, &N);
for (int i = ; i < N; i++) {
int addr;
scanf("%d", &addr);
scanf("%d%d", &List[addr].key, &List[addr].next);
}
int pre = head, addr = List[head].next, secHead = -, secAddr;
S[abs(List[head].key)] = true; while (addr != -) {
if (S[abs(List[addr].key)]) {
List[pre].next = List[addr].next;
if (secHead == -) {
secHead = addr;
secAddr = secHead;
List[secHead].next = -;
}
else {
List[secAddr].next = addr;
secAddr = addr;
List[secAddr].next = -;
}
addr = List[pre].next;
}
else {
S[abs(List[addr].key)] = true;
pre = addr;
addr = List[addr].next;
}
}
for (addr = head; addr != -; addr = List[addr].next) {
printf("%05d %d ", addr, List[addr].key);
List[addr].next == - ? printf("-1\n") : printf("%05d\n", List[addr].next);
}
for (secAddr = secHead; secAddr != -; secAddr = List[secAddr].next) {
printf("%05d %d ", secAddr, List[secAddr].key);
List[secAddr].next == - ? printf("-1\n") : printf("%05d\n", List[secAddr].next);
}
return ;
}
上一篇:kubenetes 1.9 学习 pod - volume -- dashboard


下一篇:PAT 1097 Deduplication on a Linked List[比较]