5-51 兩個有序連結串列序列的合併(Java)
阿新 • • 發佈:2019-02-01
注:此部落格不再更新,所有最新文章將發表在個人獨立部落格limengting.site。分享技術,記錄生活,歡迎大家關注
5-51 兩個有序連結串列序列的合併 (20分)
已知兩個非降序連結串列序列S1與S2,設計函式構造出S1與S2的並集新非降序連結串列S3。
輸入格式:
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1-1−1表示序列的結尾(−1-1−1不屬於這個序列)。數字用空格間隔。
輸出格式:
在一行中輸出合併後新的非降序連結串列,數字間用空格分開,結尾不能有多餘空格;若新連結串列為空,輸出NULL。
輸入樣例:
1 3 5 -1
2 4 6 8 10 -1
輸出樣例:
1 2 3 4 5 6 8 10
找錯誤:
import java.util.*; public class PTA51 { // 要改成static class ListNode class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static void main(String[] args) { ListNode l1 = getListNode(); ListNode l2 = getListNode(); // merge two lists ListNode res = mergeTwoLists(l1, l2); System.out.println(res); } private static ListNode getListNode() { Scanner in = new Scanner(System.in); List<Integer> list = new ArrayList<>(); // 用hasNextInt()會一直等待輸入,無法停止 // 而且兩次用nextInt就指向兩個不同的數,應該先存起來再輸出 /*改成: while (true) { int i = in.nextInt(); if (i == -1) break; list.add(i); } */ while (in.hasNextInt() && in.nextInt() > 0) { list.add(in.nextInt()); } int[] intList = new int[list.size()]; for (int i = 0; i < list.size(); i ++) { intList[i] = list.get(i); } return buildListNode(intList); } private static ListNode buildListNode(int[] input) { ListNode first = null, last = null, newNode; if (input.length > 0) { for (int i = 0; i < input.length && input[i] > 0; i ++) { newNode = new ListNode(input[i]); newNode.next = null; if (first == null) { first = newNode; last = newNode; } else { last.next = newNode; last = newNode; } } } return first; } public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val <= l2.val) { l1.next = mergeTwoLists(l1.next, l2); } else { l2.next = mergeTwoLists(l2.next, l1); return l2; } } }
修改完善後的程式碼:
import java.util.*; public class PTA51 { static class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public static void main(String[] args) { ListNode l1 = getListNode(); ListNode l2 = getListNode(); // merge two lists ListNode res = mergeTwoLists(l1, l2); if (res == null) { System.out.println("NULL"); return; } while (res != null) { if (res.next == null) { System.out.println(res.val); break; } System.out.print(res.val + " "); res = res.next; } } private static ListNode getListNode() { Scanner in = new Scanner(System.in); List<Integer> list = new ArrayList<>(); while (true) { int i = in.nextInt(); if (i == -1) break; list.add(i); } int[] intList = new int[list.size()]; for (int i = 0; i < list.size(); i ++) { intList[i] = list.get(i); } return buildListNode(intList); } private static ListNode buildListNode(int[] input) { ListNode first = null, last = null, newNode; if (input.length > 0) { for (int i = 0; i < input.length && input[i] > 0; i ++) { newNode = new ListNode(input[i]); newNode.next = null; if (first == null) { first = newNode; last = newNode; } else { last.next = newNode; last = newNode; } } } return first; } public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; if (l1.val <= l2.val) { l1.next = mergeTwoLists(l1.next, l2); return l1; } else { l2.next = mergeTwoLists(l2.next, l1); return l2; } } }
網上找的相似答案:
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
public class SumTwoLinkedList {
public static void main(String[] args) {
List<Integer> list1 = new LinkedList<Integer>();
Collections.addAll(list1, 30, 41, 15, 12, 56, 80);
List<Integer> list2 = new LinkedList<Integer>();
Collections.addAll(list2, 23, 56, 78, 23, 12, 33, 79, 90, 55);
test1(list1, list2);
}
public static void test1(List<Integer> list1, List<Integer> list2) {
list1.removeAll(list2);// list1中刪除和list2中交集的元素
list2.addAll(list1);// 合併
Collections.sort(list2);
for (Integer integer : list2) {
System.out.print(integer + " ");
}
}
}