5-51 两个有序链表序列的合并(Java)
来源:互联网 发布:淘宝流量钱包规则 编辑:程序博客网 时间:2024/05/16 12:26
5-51 两个有序链表序列的合并 (20分)
已知两个非降序链表序列S1与S2,设计函数构造出S1与S2的并集新非降序链表S3。
输入格式:
输入分两行,分别在每行给出由若干个正整数构成的非降序序列,用−1-1−1表示序列的结尾(−1-1−1不属于这个序列)。数字用空格间隔。
输出格式:
在一行中输出合并后新的非降序链表,数字间用空格分开,结尾不能有多余空格;若新链表为空,输出NULL。
输入样例:
1 3 5 -12 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 + " "); } }}
阅读全文
0 0
- 5-51 两个有序链表序列的合并(Java)
- 5-51 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 两个有序链表序列的合并
- 5-15 两个有序链表序列的合并
- 5-51 两个有序链表序列的合并 (20分)
- 5-51 两个有序链表序列的合并 (20分)
- 5-51 两个有序链表序列的合并(20分)
- CSS 选择器
- Linux常用命令(实用,配置tensorflow环境使用服务器GPU预备知识)
- HTML常用标签汇总、CSS常用属性汇总
- HTML基础
- Android Activity的横竖屏切换与状态保存
- 5-51 两个有序链表序列的合并(Java)
- java并发(2)ThreadLocal的使用及实现原理(使用)
- android studio java module
- 安卓开发 suspendbuttonlayout浮动按钮
- 设计模式2——工厂方法模式
- SDUT-中国象棋
- No module named win32api-for python3
- Linux内核文档翻译:Documentation/arm/Booting
- 深入理解Servlet原理