• 21. Merge Two Sorted Lists


    题目:

    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.

    链接: http://leetcode.com/problems/merge-two-sorted-lists/

    题解:合并两个排序的单链表, 创建一个dummy head,然后进行合并。 Time Complexity - O(n),Space Complexity - O(1)。

    public class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            if(l1 == null)
                return l2;
            if(l2 == null)
                return l1;
            ListNode result = new ListNode(-1);
            ListNode node = result;
            
            while(l1 != null && l2 != null){
                if(l1.val < l2.val){
                    node.next = l1;
                    l1 = l1.next;
                }
                else{
                    node.next = l2;
                    l2 = l2.next;
                }
                node = node.next;
                node.next = null;
            }
            
            if(l1 != null)
                node.next = l1;
            else if (l2 != null)
                node.next = l2;
            return result.next;
        }
    }

    二刷:

    Java:

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

    Python:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def mergeTwoLists(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            dummy = ListNode(-1)
            node = dummy
            while l1 != None and l2 != None:
                if l1.val < l2.val:
                    node.next = l1
                    l1 = l1.next
                else:
                    node.next = l2
                    l2 = l2.next
                node = node.next
            node.next = l1 if l1 != None else l2
            return dummy.next

    三刷:

    Java:

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

    Reference:

    https://leetcode.com/discuss/8372/a-recursive-solution

    https://leetcode.com/discuss/38306/simple-5-lines-python

    https://leetcode.com/discuss/45756/3-lines-c-12ms-and-c-4ms

  • 相关阅读:
    html单引号,双引号转义
    把文章里边的html标签去掉(去掉文字的样式,显示css设置的样式)
    java缓存适合使用的情况
    Java内存缓存
    springmvc怎么重定向,从一个controller跳到另一个controller
    jquery 获取标签名(tagName)
    jQuery判断checkbox是否选中的3种方法
    判断一组checkbox中是否有被选中的
    在O(N)时间内求解 正数数组中 两个数相加的 最大值
    两种方法求解 正数数组中 两个数相减 的最大值
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434625.html
Copyright © 2020-2023  润新知