题目:
思路:这个题目比较简单,有点类似升序数组。
代码:
# 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) #定义一个头结点 c1=head #让c1等于头结点,通过操作c1,最后返回头结点的后继节点 if not l1:#临界问题 return l2 if not l2 : return l1 while l1 and l2: if l1.val <= l2.val: c1.next = l1 l1=l1.next else: c1.next = l2 l2=l2.next c1=c1.next if l1: c1.next = l1 if l2: c1.next = l2 return head.next