PAT-A1097 Deduplication on a Linked List 题目内容及题解

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

题目大意

题目给定一个具有整数键的单链表L,要求删除键的绝对值重复的节点。并且要求首先输出结果链表,然后输出被删除的链表。

解题思路

  1. 建立哈希表并初始化;
  2. 读入链表节点信息,并按照访问链表方式对链表进行遍历,标注其是否有效、是否保留的信息;
  3. 对链表进行排序;
  4. 按照格式输出并返回零值。

代码

#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100010

struct Node{
    int address,data,seq,next;
    int vivid;
}node[maxn];

int HashTable[10010],first,N;
int count1=0,count2=0,p;

bool cmp(Node a,Node b){
    if(a.vivid!=b.vivid){
        return a.vivid>b.vivid;
    }else{
        return a.seq<b.seq;
    }
}

void Init(){
    int i;
    int address;
    scanf("%d%d",&first,&N);
    for(i=0;i<N;i++){
        scanf("%d",&address);
        scanf("%d%d",&node[address].data,&node[address].next);
        node[address].address=address;
    }
    p=first;
    i=0;
    while(p!=-1){
        node[p].seq=i++;
        if(HashTable[abs(node[p].data)]==0){
            node[p].vivid=2;
            count1++;
            HashTable[abs(node[p].data)]=1;
        }else{
            node[p].vivid=1;
            count2++;
        }
        p=node[p].next;
    }
    sort(node,node+maxn,cmp);
}

void Print(){
    int i=0;
    if(count1>0){
        printf("%05d %d ",node[0].address,node[0].data);
        i=1;
        while(i<count1){
            printf("%05d\n%05d %d ",node[i].address,node[i].address,node[i].data);
            i++;
        }
        printf("-1\n");
    }
    if(count2>0){
        printf("%05d %d ",node[count1].address,node[count1].data);
        i=count1+1;
        count2+=count1;
        while(i<count2){
            printf("%05d\n%05d %d ",node[i].address,node[i].address,node[i].data);
            i++;
        }
        printf("-1\n");
    }
}

int main(){
    Init();
    Print();
    return 0;
}

运行结果

PAT-A1097 Deduplication on a Linked List 题目内容及题解