LeetCode No.21 Posted on 2021-01-25 | In OJ , LeetCode | | LeetCode第二十一题题目描述将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 123456789101112131415161718示例 1:输入:l1 = [1,2,4], l2 = [1,3,4]输出:[1,1,2,3,4,4]示例 2:输入:l1 = [], l2 = []输出:[]示例 3:输入:l1 = [], l2 = [0]输出:[0] 提示:两个链表的节点数目范围是 [0, 50]-100 <= Node.val <= 100l1 和 l2 均按 非递减顺序 排列 代码1234567891011121314151617181920# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: res = ListNode(None) node = res while l1 and l2: if l1.val<l2.val: node.next,l1 = l1,l1.next else: node.next,l2 = l2,l2.next node = node.next if l1: node.next = l1 else: node.next = l2 return res.next Hobby lead creation, technology change world. Post author: StriveZs Post link: www.strivezs.com/2021/01/25/LeetCode%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E9%A2%98/ Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.