zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    jmeter接口测试 Base64加密(函数助手添加自定义函数)
    jmeter接口测试 上传文件(multipart/formdata数据请求)
    python入门_模块2
    python_元类
    python day03_ 文件处理
    Python入门day04_函数与装饰器
    python_字符编码
    三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
    JavaScriptproperty
    我在博客园的第一个博客
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434625.html
Copyright © 2011-2022 走看看