• [LeetCode] 21. 合并两个有序链表


    题目链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/

    题目描述:

    将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

    示例:

    输入:1->2->4, 1->3->4
    输出:1->1->2->3->4->4
    

    思路:

    思路1:

    迭代方法.

    每次选两个链表头结点最小的,比如:我们生活中,有两个已经按照高矮排好的队伍,我们如何把变成一个队伍!当然,每次选两个队伍排头的,比较他们的高矮!组成新的的队伍.

    时间复杂度:(O(m+n))

    空间复杂度:(O(m+n))

    思路2:

    递归方法


    关注我的知乎专栏,了解更多的解题技巧!

    代码:

    python

    迭代

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
            dummy = ListNode(0)
            p = dummy
            while l1 or l2:
                if l1 and l2:
                    tmp1 = l1.val
                    tmp2 = l2.val
                    if tmp1 < tmp2:
                        p.next = ListNode(tmp1)
                        l1 = l1.next
                    else:
                        p.next = ListNode(tmp2)
                        l2 = l2.next
                elif l1:
                    p.next = ListNode(l1.val)
                    l1 = l1.next
                elif l2:
                    p.next = ListNode(l2.val)
                    l2 = l2.next
                p = p.next
            return dummy.next
    

    java

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            ListNode dummy = new ListNode(0);
            ListNode p = dummy;
            while (l1 != null && l2 != null){
                if (l1.val < l2.val){
                    p.next = l1;
                    l1 = l1.next;
                }
                else {
                    p.next = l2;
                    l2 = l2.next;
                }
                p = p.next;
            }
            if (l1 == null) p.next = l2;
            if (l2 == null) p.next = l1;
            return dummy.next;
        }
    }
    

    递归

    python

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
            if not l1: return l2
            if not l2: return l1
            if l1.val < l2.val:
                l1.next = self.mergeTwoLists(l1.next,l2)
                return l1
            else:
                l2.next = self.mergeTwoLists(l1,l2.next)
                return l2
    

    java

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public 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(l1,l2.next);
                return l2;
            }
            
        }
    
  • 相关阅读:
    交叉编译qt5.4.1遇到的错误
    qt5.4.1官网上找不到
    Runtime dependency gstreameraudio1.0 found: NO (tried pkgconfig and cmake)
    gstreamer库 ubuntu下安装
    gstreamer编译错误
    gstreamer在ubuntu上源码编译
    http学习
    SQL定义变量
    对w3wp.exe的一点认识!
    官方发布!DevExpress WPF 2022年产品升级路线图
  • 原文地址:https://www.cnblogs.com/powercai/p/10775175.html
Copyright © 2020-2023  润新知