/**
* @ClassName MyList
* @Description: TODO
* @Author ZK
* @Date 2020/8/24 17:02
* @Version V1.0
**/
public class MyList {
//使用递归的方法
public static Node Merge1(Node head1, Node head2) {
//若两个链表都为空,返回null
if (head1 == null && head2 == null) return null;
//若其中一个链表为空,返回另外一个链表
if (head1 == null) return head2;
if (head2 == null) return head1;
//判断两个链表是否相交
if (head1 == head2) {
System.out.println("两个链表相交!!!!!!!");
return null;
}
//定义一个空的链表
Node head = null;
//若head1中的当前值小于等于head2的当前值时:
// 1.把head1的赋值给head
// 2.递归调用Merge1(head1.next, head2)
//否则
// 1.把head2的赋值给head
// 2.递归调用Merge1(head1, head2.next)
if (head1.data <= head2.data) {
head = head1;
head.next = Merge1(head1.next, head2);
} else {
head = head2;
head.next = Merg
两个有序链表合并为一个有序的新链表
最新推荐文章于 2022-07-21 11:05:57 发布