A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key
and a Next
pointer to the next structure. Now given a linked list, you are supposed to sort the structures according to their key values in increasing order.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive N (<105) and an address of the head node, where N is the total number of nodes in memory and the address of a node is a 5-digit positive integer. NULL is represented by −1.
Then N lines follow, each describes a node in the format:
Address Key Next
where Address
is the address of the node in memory, Key
is an integer in [−105,105], and Next
is the address of the next node. It is guaranteed that all the keys are distinct and there is no cycle in the linked list starting from the head node.
Output Specification:
For each test case, the output format is the same as that of the input, where N is the total number of nodes in the list and all the nodes must be sorted order.
Sample Input:
5 00001
11111 100 -1
00001 0 22222
33333 100000 11111
12345 -1 33333
22222 1000 12345
Sample Output:
5 12345
12345 -1 00001
00001 0 11111
11111 100 22222
22222 1000 33333
33333 100000 -1
解题思路:
题目大意:给一个单链表,将其按结点data域排序并输出。
将链表有序串通放入list中,对list实现Comparable接口将list排序,最后修改链表next域并输出。
注意:因为输入样例中存在无效结点,需要对链表先串通提取至list再对其操作。测试样例4中输入均为无效结点,需要特判,输出 "0 ,-1" 。测试点3:在对最终的链表首地址输出时,同样要注意输出的格式 :“%05d”。
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 n = Integer.parseInt(split[0]);
int adressFirst = Integer.parseInt(split[1]);
Node1052[]node = new Node1052[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 Node1052(adress, data, next);
}
int temp = adressFirst;
List<Node1052> list = new ArrayList<Node1052>();
while(temp != -1) {
list.add(node[temp]);
temp = node[temp].next;
}
Collections.sort(list);
temp = -1;
for(int i = list.size() - 1;i >= 0;i--) {
list.get(i).next = temp;
temp = list.get(i).adress;
}
if(list.isEmpty()) {
System.out.println("0" + " " + "-1");
}else {
System.out.println(list.size() + " " + String.format("%05d", list.get(0).adress));
for(Node1052 data : list) {
System.out.println(data);
}
}
}
}
class Node1052 implements Comparable<Node1052>{
int adress;
int data;
int next;
public Node1052(int adress, int data, int next) {
this.adress = adress;
this.data = data;
this.next = next;
}
@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("%2d", next);
}
}
@Override
public int compareTo(Node1052 o) {
return this.data -o.data;
}
}
PAT提交截图: