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
解题思路:
题目大意:给定一个单链表,只保留首先出现的data域的绝对值的结点,将后出现的移除至另一链表,最后输出这两条单链表。
将输入的链表有序移至list,遍历list时,需要用到哈希表存储已出现元素,因为题中已经给定data值得范围,可以用数组模拟哈希表,将每次首次出现的data的绝对值移至哈希表,遍历元素时查看哈希表中是否已经存在,若已存在,则移出至另一list,最后修改两个链表的next域并输出。
java代码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
int adressFirst = Integer.parseInt(split[0]);
int n = Integer.parseInt(split[1]);
Node1097[]node = new Node1097[100005];
for(int i = 0; i < n;i++) {
split = br.readLine().split(" ");
int adress = Integer.parseInt(split[0]);
int data = Integer.parseInt(split[1]);
int next = Integer.parseInt(split[2]);
node[adress] = new Node1097(adress, data, next);
}
int temp = adressFirst;
List<Node1097> list = new ArrayList<Node1097>();
while(temp != -1) {
list.add(node[temp]);
temp = node[temp].next;
}
int []arr = new int[10005];
List<Node1097> list1 = new ArrayList<Node1097>();
List<Node1097> list2 = new ArrayList<Node1097>();
for(int i = 0; i < list.size();i++) {
if(arr[list.get(i).dataAbosolute] == 0) {
list1.add(list.get(i));
arr[list.get(i).dataAbosolute]++;
}else {
list2.add(list.get(i));
}
}
temp = -1;
for(int i = list1.size() - 1;i >= 0;i--) {
list1.get(i).next = temp;
temp = list1.get(i).adress;
}
temp = -1;
for(int i = list2.size() - 1;i >= 0;i--) {
list2.get(i).next = temp;
temp = list2.get(i).adress;
}
for(Node1097 data : list1) {
System.out.println(data);
}
for(Node1097 data : list2) {
System.out.println(data);
}
}
}
class Node1097{
int adress;
int data;
int dataAbosolute;
int next;
public Node1097(int adress, int data, int next) {
this.adress = adress;
this.data = data;
this.next = next;
dataAbosolute = Math.abs(data);
}
@Override
public String toString() {
if(this.next != -1) {
return String.format("%05d", adress) + " " + data + " " + String.format("%05d", next);
}else {
return String.format("%05d", adress) + " " + data + " " + String.format("%02d", next);
}
}
}
PAT提交截图: