题目1519:合并两个排序的链表
时间限制:1 秒内存限制:128 兆特殊判题:否提交:1921解决:873
题目描述:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
(hint: 请务必使用链表。)
输入:
输入可能包含多个测试样例,输入以EOF结束。
对于每个测试案例,输入的第一行为两个整数n和m(0<=n<=1000, 0<=m<=1000):n代表将要输入的第一个链表的元素的个数,m代表将要输入的第二个链表的元素的个数。
下面一行包括n个数t(1<=t<=1000000):代表链表一中的元素。接下来一行包含m个元素,s(1<=t<=1000000)。
输出:
对应每个测试案例,
若有结果,输出相应的链表。否则,输出NULL。
样例输入:
5 2
1 3 5 7 9
2 4
0 0
样例输出:
1 2 3 4 5 7 9
NULL
题目很简单,用三个指针分别指向合并前的链表A、B及合并后的链表C,通过判断大小来移动C的位置,C指向A、B中的小值,然后移动指向小值的指针A或B,注意A、B不一定等长(此处隐含了A、B有可能为空)
贴代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
String[] a = line.split(" ");
int n = Integer.parseInt(a[0]);
int m = Integer.parseInt(a[1]);
List listA = new List();
if (n > 0) {
line = br.readLine();
a = line.split(" ");
for (int i = 0; i < n; i++) {
listA.add(Integer.parseInt(a[i]));
}
}
List listB = new List();
if (m > 0) {
line = br.readLine();
a = line.split(" ");
for (int i = 0; i < m; i++) {
listB.add(Integer.parseInt(a[i]));
}
}
List result = new List();
result = result.merge(listA, listB);
result.printList();
}
}
public static class List {
private static class Node {
public Node next;
public int data;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
private Node head;
private int theSize;
private Node last;
public List() {
head = new Node(0, null);
last = head;
theSize = 0;
}
public int size() {
return theSize;
}
public void printList() {
if (size() > 0) {
Node curr = head;
StringBuilder sb = new StringBuilder();
while (curr.next != null) {
curr = curr.next;
sb.append(curr.data + " ");
}
System.out.println(sb.substring(0, sb.length() - 1));
} else {
System.out.println("NULL");
}
}
public void add(int data) {
Node node = new Node(data, null);
last.next = node;
last = node;
theSize++;
}
public List merge(List listA, List listB) {
Node curr = head;
Node nodeA = listA.head.next;
Node nodeB = listB.head.next;
while (nodeA != null && nodeB != null) {
if (nodeA.data <= nodeB.data) {
curr.next = nodeA;
curr = curr.next;
nodeA = nodeA.next;
} else {
curr.next = nodeB;
curr = curr.next;
nodeB = nodeB.next;
}
theSize++;
}
if (nodeA != null) {
curr.next = nodeA;
theSize++;
}
if (nodeB != null) {
curr.next = nodeB;
theSize++;
}
return this;
}
}
}
/**************************************************************
Problem: 1519
User: ppulse
Language: Java
Result: Accepted
Time:2380 ms
Memory:76636 kb
****************************************************************/
坑爹的总是超时,查了很久最后定位原因主要是用 Scanner
太慢,核心代码不变,将 Scanner
换成 BufferedReader
解决问题。
用 Scanner
的代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
int m = in.nextInt();
List listA = new List();
for (int i = 0; i < n; i++) {
listA.add(in.nextInt());
}
List listB = new List();
for (int i = 0; i < m; i++) {
listB.add(in.nextInt());
}
List result = new List();
result = result.merge(listA, listB);
result.printList();
}
}
public static class List {
private class Node {
public Node next;
public int data;
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
}
private Node head;
private int theSize;
private Node last;
public List() {
head = new Node(0, null);
last = head;
theSize = 0;
}
public int size() {
return theSize;
}
public void printList() {
if (size() > 0) {
Node curr = head;
StringBuilder sb = new StringBuilder();
while (curr.next != null) {
curr = curr.next;
sb.append(curr.data + " ");
}
System.out.println(sb.substring(0, sb.length() - 1));
} else {
System.out.println("NULL");
}
}
public void add(int data) {
Node node = new Node(data, null);
last.next = node;
last = node;
theSize++;
}
public List merge(List listA, List listB) {
Node curr = head;
Node nodeA = listA.head.next;
Node nodeB = listB.head.next;
while (nodeA != null && nodeB != null) {
if (nodeA.data <= nodeB.data) {
curr.next = nodeA;
curr = curr.next;
nodeA = nodeA.next;
} else {
curr.next = nodeB;
curr = curr.next;
nodeB = nodeB.next;
}
theSize++;
}
while (nodeA != null) {
curr.next = nodeA;
curr = curr.next;
nodeA = nodeA.next;
theSize++;
}
while (nodeB != null) {
curr.next = nodeB;
curr = curr.next;
nodeB = nodeB.next;
theSize++;
}
return this;
}
}
}
/**************************************************************
Problem: 1519
User: ppulse
Language: Java
Result: Time Limit Exceed
****************************************************************/