zoukankan      html  css  js  c++  java
  • lintcode:合并两个排序链表

    题目:

    合并两个排序链表

    将两个排序链表合并为一个新的排序链表

     样例

    给出 1->3->8->11->15->null,2->null,

    返回 1->2->3->8->11->15->null。

    解题:

    数据结构中的书上说过,可解,异步的方式移动两个链表的指针,时间复杂度O(n+m)

    Java程序:

    /**
     * Definition for ListNode.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int val) {
     *         this.val = val;
     *         this.next = null;
     *     }
     * }
     */ 
    public class Solution {
        /**
         * @param ListNode l1 is the head of the linked list
         * @param ListNode l2 is the head of the linked list
         * @return: ListNode head of linked list
         */
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            // write your code here
            if(l1==null && l2!=null)
                return l2;
            if(l1!=null && l2==null)
                return l1;
            if(l1==null && l2==null)
                return null;
            ListNode head = new ListNode(0);
            ListNode current = head;
            
          
            while(l1!=null && l2!=null){
                    if(l1.val<=l2.val){
                        current.next = l1;
                        current = current.next;
                        l1 = l1.next;
                    }else{
                        current.next = l2;
                        current = current.next;
                        l2 = l2.next;
                    }
                }
    
            if(l1!=null)
                current.next= l1;
            if(l2!=null)
                current.next=l2;
            return head.next;
        }
    }
    View Code

    总耗时: 13348 ms

    Python程序:

    """
    Definition of ListNode
    class ListNode(object):
        def __init__(self, val, next=None):
            self.val = val
            self.next = next
    """
    class Solution:
        """
        @param two ListNodes
        @return a ListNode
        """
        def mergeTwoLists(self, l1, l2):
            # write your code here
            if l1==None:
                return l2
            if l2==None:
                return l1
            if l1==None and l2==None:
                return None
            head = ListNode(0)
            p = head
            while l1!=None and l2!=None:
                if l1!=None and l2!=None:
                    if l1.val<= l2.val:
                        p.next = l1
                        p = p.next
                        l1 = l1.next
                    else:
                        p.next = l2
                        p = p.next
                        l2 = l2.next
                if l1==None:
                    p.next = l2
                    break
                if l2==None:
                    p.next = l1
                    break
            return head.next
    View Code

    总耗时: 2032 ms

     参考剑指OfferP117

    利用递归的思想

    小的节点链接,下一次递归

    递归的好处是不用单独搞个节点当作头节点了,通俗点说是许多头节点连接起来的,最终我们返回的是第一个头节点

    /**
     * Definition for ListNode.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int val) {
     *         this.val = val;
     *         this.next = null;
     *     }
     * }
     */ 
    public class Solution {
        /**
         * @param ListNode l1 is the head of the linked list
         * @param ListNode l2 is the head of the linked list
         * @return: ListNode head of linked list
         */
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            // write your code here
            if(l1==null && l2!=null)
                return l2;
            if(l1!=null && l2==null)
                return l1;
            if(l1==null && l2==null)
                return null;
            ListNode MergeHead = null;
            if(l1.val < l2.val){
                MergeHead = l1;
                MergeHead.next = mergeTwoLists(l1.next,l2);
            }else{
                MergeHead = l2;
                MergeHead.next = mergeTwoLists(l1,l2.next);
            }
            return MergeHead;
            
        }
    }
    Java Code

    总耗时: 14047 ms

    """
    Definition of ListNode
    class ListNode(object):
        def __init__(self, val, next=None):
            self.val = val
            self.next = next
    """
    class Solution:
        """
        @param two ListNodes
        @return a ListNode
        """
        def mergeTwoLists(self, l1, l2):
            # write your code here
            if l1==None:
                return l2
            if l2==None:
                return l1
            if l1==None and l2==None:
                return None
            head = None
            if l1.val< l2.val:
                head = l1
                head.next = self.mergeTwoLists(l1.next,l2)
            else:
                head = l2
                head.next = self.mergeTwoLists(l1,l2.next)
            return head 
    Python Code

    总耗时: 2403 ms

  • 相关阅读:
    4.qml-ECMAScript(Array对象、Math对象)
    3.qml-ECMAScript_03(Object基类对象、String对象)
    2.qml-ECMAScript_02(原始值类型、通用转换方法)
    ORA-00001: 违反唯一约束条件(SOLEX.SYS_C0012537) --解决方法
    macOS 系统打开和退出文件夹(cd命令)
    macOS 系统下node安装和环境配置(通过node官网)
    macOS 系统报错:zsh:command not found :npm
    macOS 系统更新node老是不成功
    macOS 系统上升级 Python
    maxOS 系统更新node版本
  • 原文地址:https://www.cnblogs.com/theskulls/p/4870694.html
Copyright © 2011-2022 走看看