• [leetcode]21. Merge Two Sorted Lists


    突然发现代码开头的注释取消注释就是ListNode的定义,可以本地调试了。。。

    这道题注意两个链表可以同时为None.

    Runtime: 44 ms, faster than 86.67% of Python3 online submissions forMerge Two Sorted Lists.
    Memory Usage: 13 MB, less than 86.39% of Python3 online submissions forMerge Two Sorted Lists.
     

    Submission Detail

    208 / 208 test cases passed.
    Status: 

    Accepted

    Runtime: 44 ms
    Memory Usage: 13 MB
    Submitted: 0 minutes ago
    class Solution:
        def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
            #0
            if l1 == None and l2 == None:
                return l1
            if l1 == None:
                return l2
            if l2 == None:
                return l1
            # init
            if l1.val >= l2.val:
                ret = ListNode(l2.val)
                ret.next = None
                l2 = l2.next
            else:
                ret = ListNode(l1.val)
                ret.next = None
                l1 = l1.next
            retu =ret
            # normal
            while (l1 != None or l2 != None):
                # none
                if l1 == None:
                    ret.next = l2
                    return retu
                if l2 == None:
                    ret.next = l1
                    return retu
                # normal
                if l1.val >= l2.val:
                    ret.next = ListNode(l2.val)
                    ret = ret.next
                    l2 = l2.next
                else:
                    ret.next = ListNode(l1.val)
                    ret = ret.next
                    l1 = l1.next
            return retu
  • 相关阅读:
    366. Find Leaves of Binary Tree
    369. Plus One Linked List
    370. Range Addition
    411. Minimum Unique Word Abbreviation
    379. Design Phone Directory
    Permutation
    leetcode216-Combination Sum III
    百度star编程赛-练习1
    腾讯暑期实习生面试题
    素数筛选
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10934419.html
Copyright © 2020-2023  润新知