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

    Example:

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

    # 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 = cur = ListNode(None)
            while l1 and l2:
                if l1.val <= l2.val:
                    cur.next = ListNode(l1.val)
                    l1 = l1.next
                else:
                    cur.next = ListNode(l2.val)
                    l2 = l2.next
                cur = cur.next
            l = l1 or l2
            while l:
                cur.next = ListNode(l.val)
                l = l.next
                cur = cur.next
            return dummy.next 
    

    可以稍微少几行代码:

    # 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 = cur = ListNode(None)
            while l1 or l2:
                if (l1 and l2 and l1.val <= l2.val) or l2 is None:
                    cur.next = ListNode(l1.val)
                    l1 = l1.next
                elif (l1 and l2 and l1.val > l2.val) or l1 is None:
                    cur.next = ListNode(l2.val)
                    l2 = l2.next
                cur = cur.next        
            return dummy.next
     
    
    # 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
            """
            if (l1 and l2 and l1.val <= l2.val) or (l1 and not l2):
                node = ListNode(l1.val)
                node.next = self.mergeTwoLists(l1.next, l2)
                return node
            elif (l1 and l2 and l1.val > l2.val) or (l2 and not l1):
                node = ListNode(l2.val)
                node.next = self.mergeTwoLists(l1, l2.next)
                return node
            else:                
                return None
     
    

    还有偷懒的解法:

    class Solution {
    public:
        ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
            ListNode dummy(INT_MIN);
            ListNode *tail = &dummy;
            
            while (l1 && l2) {
                if (l1->val < l2->val) {
                    tail->next = l1;
                    l1 = l1->next;
                } else {
                    tail->next = l2;
                    l2 = l2->next;
                }
                tail = tail->next;
            }
    
            tail->next = l1 ? l1 : l2;
            return dummy.next;
        }
    };
    

     看到其他人的解法,如果允许修改原有list的话,还是可以的:

    # 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 = cur = ListNode(None)
            while l1 and l2:
                if l1.val < l2.val:
                    cur.next = l1
                    l1 = l1.next
                else:
                    cur.next = l2
                    l2 = l2.next
                cur = cur.next
            cur.next = l1 or l2
            return dummy.next
     
    

     对应的递归解法:

    # 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
            """
            if l1 is None: return l2
            if l2 is None: 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        
    
  • 相关阅读:
    内容页超连接关键词的完美实现
    鼠标经过文字链接时出现漂亮的提示层
    简单的jQuery检测注册用户名
    触发Repeater中的Dropdownlist的SelectedIndexChanged如何获得Repeater的当前行
    读取XML的节点属性并绑定到ListBox
    第十八章 6string型字符串的替换 简单
    第十八章 2string字符串 简单
    第十七章 特殊成员_函数指针也可以做为参数 简单
    第十七章 特殊成员_类的函数指针 简单
    第十七章 特殊成员_成员函数指针数组 简单
  • 原文地址:https://www.cnblogs.com/bonelee/p/8836225.html
Copyright © 2011-2022 走看看