LeetCood 21. Merge Two Sorted Lists-合并两个有序的链表(JAVA)

题目理解

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

合并两个排序好的链表,并返回新链表。新链表由这两个链表的头部拼接而成

思路分析

  1. 先判断l1,l2是够存在空链表Null,有了直接返回;否则进入2
  2. 找到l1,l2头节点值最小的,作为结果链表的头节点
  3. 依次遍历l1,l2,将较小的插入到结果链表的后面

通过步骤分析,在实现时,可以使用非递归和递归两种方法实现。

代码实现

  • 非递归方法
/**
     * 非递归实现
     * 
     * 合并两个排好序的链表,并返回新链表
     * 新链表应该由这两个链表的头部拼接而成。
     * 
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        /**
         * 思路
         * 判断是否有存在空链表Null,有了直接返回
         * 找到l1,l2中最小的,做为结果链表的头结点
         * 依次遍历l1,l2,将较小的插入结果链表的后面
         */

        if(l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1 == null && l2 == null) {
            return null;
        }

        ListNode p1 = l1;
        ListNode p2 = l2;
        ListNode result = null; //新建链表,用于存放就结果

        //找到l1,l2中最小的头部,作为新链表的头结点
        if (l1.val < l2.val) {
            result = l1;
            p1 = p1.next;
        } else {
            result = l2;
            p2 = p2.next;
        }

        ListNode ps = result;

        //当两个链表都不为空时,对比,将小的插入新链表后面,链表顺序往下移动
        while (p1 != null && p2 != null) {
            if (p1.val < p2.val) {
                ps.next = p1;
                ps = ps.next;
                p1 = p1.next;
            } else {
                ps.next = p2;
                ps = ps.next;
                p2 = p2.next;
            }
        }

        //判断那个链表为空,将另外一个不为空的直接插入新链表的后面即可
        if (p1 == null && p2 ==null) {
            return result;
        } else if (p1 == null && p2 != null){
            ps.next = p2;
        } else if (p1 != null && p2 ==null) {
            ps.next = p1;
        }

        return result;
    }

  • 递归方法
/**
     * 递归解法
     * @param l1
     * @param l2
     * @return
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        if (l1.val < l2.val) {
            ListNode rs = l1;
            rs.next = mergeTwoLists01(l1.next,l2);
            return rs;
        } else {
            ListNode rs = l2;
            rs.next = mergeTwoLists01(l1,l2.next);
            return rs;
        }

    }

小知识大道理,一点一滴积累。给点个赞吧微笑

(个人微信公众号:TestOnTheRoad,搜索ID或者扫描下方二维码添加关注,关注测试开发工程师的成长之路

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值