1)
两个列表逐个对比,谁小接谁。
当有列表遍历结束时,则剩下全部列表接在末尾
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: head = ListNode(0) temp = head while l1 and l2: if l1.val < l2.val: temp.next = ListNode(l1.val) l1 = l1.next else: temp.next = ListNode(l2.val) l2 = l2.next temp = temp.next if l1: temp.next = l1 else: temp.next = l2 return head.next
原文:https://www.cnblogs.com/cbachen/p/14841887.html