16.合并两个排序的链表 Java

本文详细介绍了如何合并两个单调递增的链表,并保持合并后链表的单调不减规则。提供了递归和非递归两种解法的代码实现,同时讨论了如何将此方法应用于合并K个排序链表的问题。
题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

解题思路

两种解法:递归和非递归

参考代码
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
//递归解法
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)
            return list2;
        else if(list2 == null)
            return list1;
        ListNode mergehead = null;
        if(list1.val <= list2.val){
            mergehead = list1;
            mergehead.next = Merge(list1.next,list2);
        }else{
            mergehead = list2;
            mergehead.next = Merge(list1, list2.next);
        }
        return mergehead;
    }
}
//非递归解法
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1 == null)
            return list2;
        else if(list2 == null)
            return list1;
        ListNode mergehead = null;
        if(list1.val <= list2.val){
            mergehead = list1;
            list1 = list1.next;
        }else{
            mergehead = list2;
            list2 = list2.next;
        }
        ListNode cur = mergehead;
        while(list1 != null && list2 != null){
            if(list1.val <= list2.val){
                cur.next = list1;
                list1 = list1.next;
            }else{
                cur.next = list2;
                list2 = list2.next;
            }
            cur = cur.next;
        }
        if(list1 == null)
            cur.next = list2;
        else if(list2 == null)
            cur.next = list1;
        return mergehead;
    }
}



同时有一个和这个题类似的题

题目:合并K个排序链表

题目描述

合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。

示例:

输入: [ 1->4->5, 1->3->4, 2->6 ] 输出: 1->1->2->3->4->4->5->6

思路

想办法转成合并两个排序链表再做

代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 * ListNode(int x) { val = x; }  * }  */ class Solution { public ListNode mergeKLists(ListNode[] lists) { if(lists.length==0){ return null; } else if(lists.length==1){ return lists[0]; } else if(lists.length==2){ return mergeTwoList(lists[0],lists[1]); }else{ ListNode[] l1=new ListNode[lists.length/2]; ListNode[] l2=new ListNode[lists.length-lists.length/2]; for(int i=0;i<lists.length;i++){ if(i<lists.length/2) l1[i]=lists[i]; else l2[i-lists.length/2]=lists[i]; } return mergeTwoList(mergeKLists(l1),mergeKLists(l2)); } } public ListNode mergeTwoList(ListNode l1,ListNode l2){ if(l1==null){ return l2; } if(l2==null){ return l1; } if(l1.val>=l2.val){ l2.next=mergeTwoList(l1,l2.next); return l2; }else{ l1.next=mergeTwoList(l1.next,l2); return l1; } } }
 

转载于:https://www.cnblogs.com/feicheninfo/p/10535260.html

### Java 实现合并两个无序链表Java 中,可以创建一个 `LinkedList` 类来表示单向链表节点,并通过遍历两个链表的方式将其合并。由于题目提到的是 **无序链表** 的合并操作,因此可以直接将第二个链表的所有节点追加到第一个链表之后。 以下是具体的实现方式: #### 定义链表结构 首先定义链表节点类 `ListNode` 和辅助函数用于构建链表以及打印结果。 ```java class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; this.next = null; } } ``` #### 合并逻辑 编写一个方法 `mergeTwoUnorderedLists` 来完成两个无序链表合并操作。此过程可以通过简单的指针移动实现。 ```java public class LinkedListMerger { public static ListNode mergeTwoUnorderedLists(ListNode l1, ListNode l2) { if (l1 == null && l2 == null) return null; // 如果两者都为空,则返回null if (l1 == null) return l2; // 如果l1为空,直接返回l2 if (l2 == null) return l1; // 如果l2为空,直接返回l1 ListNode dummyHead = new ListNode(-1); // 创建虚拟头结点以便于处理边界情况 ListNode tail = dummyHead; // 尾部指针初始化为dummyHead while (l1 != null || l2 != null) { // 当任意一个链表不为空时继续循环 if (l1 != null) { // 添加l1中的所有节点 tail.next = l1; tail = tail.next; l1 = l1.next; } if (l2 != null) { // 继续添加l2中的所有节点 tail.next = l2; tail = tail.next; l2 = l2.next; } } return dummyHead.next; // 返回新的链表头部(跳过虚拟头结点) } public static void printList(ListNode head) { StringBuilder sb = new StringBuilder(); while (head != null) { sb.append(head.val).append(" -> "); head = head.next; } sb.append("null"); System.out.println(sb.toString()); } public static void main(String[] args) { // 构建测试数据 ListNode l1 = new ListNode(1); l1.next = new ListNode(3); l1.next.next = new ListNode(5); ListNode l2 = new ListNode(2); l2.next = new ListNode(4); l2.next.next = new ListNode(6); // 执行合并操作 ListNode mergedList = mergeTwoUnorderedLists(l1, l2); // 输出结果 printList(mergedList); // 预期输出: 1 -> 3 -> 5 -> 2 -> 4 -> 6 -> null } } ``` 上述代码实现了两个无序链表的简单拼接功能[^1]。如果需要进一步优化或者按照特定顺序排列,则需引入额外的排序算法或调整策略[^2]。 --- ### 复杂度分析 - 时间复杂度:O(n + m),其中 n 是第一个链表长度,m 是第二个链表长度。整个过程中只需一次遍历即可完成。 - 空间复杂度:O(1),除了存储最终结果外并未使用其他额外空间。 --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值