码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode——21. 合并两个有序链表

时间:2019-10-23 19:51:28      阅读:60      评论:0      收藏:0      [点我收藏+]

标签:efi   solution   链表   link   pytho   code   节点   get   完成   

我还是自己没有完成。

先建立新节点,然后进行操作。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        p=rst=ListNode(0)
        while True:
            try:
                while l1.val<=l2.val:
                    p.next=l1
                    l1,p=l1.next,p.next
                while l1.val>l2.val:
                    p.next=l2
                    l2,p=l2.next,p.next
            except:break
        p.next=l1 or l2
        return rst.next
执行用时 :44 ms, 在所有 python3 提交中击败了95.09%的用户
内存消耗 :13.9 MB, 在所有 python3 提交中击败了5.66%的用户
 
                                                                                                         ——2019.10.23

 

leetcode——21. 合并两个有序链表

标签:efi   solution   链表   link   pytho   code   节点   get   完成   

原文地址:https://www.cnblogs.com/taoyuxin/p/11728315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!