将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4
class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ h = ListNode(-1) cur = h cur1 = l1 cur2 = l2 while cur1 != None and cur2 != None: if cur1.val <= cur2.val: cur.next = cur1 cur1 = cur1.next else: cur.next = cur2 cur2 = cur2.next cur = cur.next if cur1 != None: cur.next = cur1 if cur2 != None: cur.next = cur2 return h.next
原文:https://www.cnblogs.com/turningli/p/12483933.html