1. # 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
    2. #
    3. #
    4. #
    5. # 示例 1:
    6. #
    7. #
    8. # 输入:l1 = [1,2,4], l2 = [1,3,4]
    9. # 输出:[1,1,2,3,4,4]
    10. #
    11. #
    12. # 示例 2:
    13. #
    14. #
    15. # 输入:l1 = [], l2 = []
    16. # 输出:[]
    17. #
    18. #
    19. # 示例 3:
    20. #
    21. #
    22. # 输入:l1 = [], l2 = [0]
    23. # 输出:[0]
    24. #
    25. #
    26. #
    27. #
    28. # 提示:
    29. #
    30. #
    31. # 两个链表的节点数目范围是 [0, 50]
    32. # -100 <= Node.val <= 100
    33. # l1 和 l2 均按 非递减顺序 排列
    34. #
    35. # Related Topics 递归 链表
    36. # 👍 1469 👎 0
    37. # leetcode submit region begin(Prohibit modification and deletion)
    38. # Definition for singly-linked list.
    39. # class ListNode(object):
    40. # def __init__(self, val=0, next=None):
    41. # self.val = val
    42. # self.next = next
    43. class Solution(object):
    44. def mergeTwoLists(self, l1, l2):
    45. """
    46. :type l1: ListNode
    47. :type l2: ListNode
    48. :rtype: ListNode
    49. """
    50. dummyHead = ListNode(0)
    51. point = dummyHead
    52. while l1 or l2:
    53. if (l1 and l2 and l1.val <= l2.val) or (not l2):
    54. point.next = ListNode(l1.val)
    55. l1 = l1.next
    56. else:
    57. point.next = ListNode(l2.val)
    58. l2 = l2.next
    59. point = point.next
    60. return dummyHead.next
    61. # leetcode submit region end(Prohibit modification and deletion)