1.题目要求删除重复的节点,并把重复的节点建立成一个新的链表,最终输出两个链表,一个是去重的链表,一个是重复节点组成的链表。
2.主要是考察链表的删除和建表操作,使用map进行记录已经存在过的链表节点。
3.注意是删除绝对值相同的节点。
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 (<= 105) 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 104, 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
AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
00100 5
99999 -7 87654
23854 -15 00000
87654 15 -1
00000 -15 99999
00100 21 23854
00001 2
00002 1 -1
00001 1 00002
*/
struct ListNode{
int val, add;
ListNode* next;
ListNode() :val(-1), add(-1), next(NULL){};
ListNode(int x,int a) :val(x), add(a), next(NULL){};
};
int main(void)
{
ListNode *list = new ListNode[100000];
map<int, bool> exist;
int headAdd, n;
cin >> headAdd >> n;
for (int i = 0; i < n; i++)
{
int pre, val, next;
scanf("%d %d %d", &pre, &val, &next);
list[pre].val = val;
list[pre].add = pre;
if (next != -1)
{
list[next].add = next;
list[pre].next = &list[next];
}
else
list[pre].next = NULL;
}
ListNode*head = &list[headAdd];
ListNode*preHead = head;
ListNode*newList = new ListNode(-1, -1);
ListNode*newListHead = newList;
while (head != NULL)
{
if (exist[abs(head->val)])
{
preHead->next = head->next;
newList->next = head;//
newList = newList->next;//newList现在指向head
head = preHead->next;
newList->next = NULL;
}
else
{
exist[abs(head->val)] = true;
preHead = head;
head = head->next;
}
}
head = &list[headAdd];
while (head != NULL)
{
printf("%05d %d ", head->add, head->val);
if (head->next != NULL)
printf("%05d\n", head->next->add);
else
printf("-1\n");
head = head->next;
}
head = newListHead->next;
while (head != NULL)
{
printf("%05d %d ", head->add, head->val);
if (head->next != NULL)
printf("%05d\n", head->next->add);
else
printf("-1\n");
head = head->next;
}
return 0;
}
[/c]