5-51 两个有序链表序列的合并(Java)

注:此博客不再更新,所有最新文章将发表在个人独立博客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 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 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 list1 = new LinkedList();
        Collections.addAll(list1, 30, 41, 15, 12, 56, 80);
        List list2 = new LinkedList();
        Collections.addAll(list2, 23, 56, 78, 23, 12, 33, 79, 90, 55);
        test1(list1, list2);
	}
	
    public static void test1(List list1, List list2) {
        list1.removeAll(list2);// list1中删除和list2中交集的元素
        list2.addAll(list1);// 合并
        Collections.sort(list2);
        for (Integer integer : list2) {
            System.out.print(integer + " ");
        }
    }
}

你可能感兴趣的:(PTA)