- 题目描述:
-
建立一个升序链表并遍历输出。
- 输入:
-
输入的每个案例中第一行包括1个整数:n(1<=n<=1000),接下来的一行包括n个整数。
- 输出:
-
可能有多组测试数据,对于每组数据,
将n个整数建立升序链表,之后遍历链表并输出。
- 样例输入:
-
4 3 5 7 9
- 样例输出:
-
3 5 7 9
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.List;
import java.util.LinkedList;
import java.util.Collections;
import java.util.Iterator;
class Main
{
public static final boolean DEBUG = false;
public static void main(String[] args) throws IOException
{
Scanner cin;
int n;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
while (cin.hasNext()) {
n = cin.nextInt();
List<Integer> l = new LinkedList<Integer>();
for (int i = 0; i < n; i++) {
int num = cin.nextInt();
l.add(num);
}
Collections.sort(l);
Iterator<Integer> it = l.iterator();
boolean first = true;
while (it.hasNext()) {
if (first) first = false;
else System.out.print(" ");
System.out.print(it.next());
}
System.out.println();
}
}
}