Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤105) which is the total number of nodes, and a positive K (≤103). 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 Data Next
Sample Input:
00100 9 10
23333 10 27777
00000 0 99999
00100 18 12309
68237 -6 23333
33218 -4 00000
48652 -2 -1
99999 5 68237
27777 11 48652
12309 7 33218
Sample Output:
33218 -4 68237
68237 -6 48652
48652 -2 12309
12309 7 00000
00000 0 99999
99999 5 23333
23333 10 00100
00100 18 27777
27777 11 -1
解题思路:
题目大意:给定一单链表将其重新排列并输出,排列方式为按原单链表顺序输出0之前的data,再输出【0,k】的data,再将剩余元素输出。
可以在对原单链表遍历时先将正常顺序放入一个list中,再对list遍历时造三个list分别存储三种情况,遍历完合并至同一list中,最后对最终链表通过指针链接。
注意有无效结点的输入,通过while可过滤,遍历时用list.size()求长度,不要用所给的n。
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]);
int rage = Integer.parseInt(split[2]);
Node1133 []node = new Node1133[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 Node1133(adress, data, next);
}
int temp = adressFirst;
List<Node1133> list = new ArrayList<Node1133>();
while(temp != -1) {
list.add(node[temp]);
temp = node[temp].next;
}
List<Node1133> list1 = new ArrayList<Node1133>();
List<Node1133> list2 = new ArrayList<Node1133>();
List<Node1133> list3 = new ArrayList<Node1133>();
for(int i = 0; i < list.size() ;i++) {
if(list.get(i).data < 0) {
list1.add(list.get(i));
}else if(list.get(i).data <= rage) {
list2.add(list.get(i));
}else {
list3.add(list.get(i));
}
}
list1.addAll(list2);
list1.addAll(list3);
temp = -1;
for(int i = list1.size() - 1; i >= 0;i--) {
list1.get(i).next = temp;
temp = list1.get(i).adress;
}
for(Node1133 data : list1) {
System.out.println(data);
}
}
}
class Node1133 {
int adress;
int data;
int next;
public Node1133(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);
}
}
}
PAT提交截图: