题目
关键词
链表
代码记录
# 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:
import math
godnode=ListNode(-math.inf)
cur=godnode
while l1 and l2:
if l1.val <= l2.val:
cur.next=l1
l1=l1.next
else:
cur.next=l2
l2=l2.next
cur.next.next=None
cur=cur.next
if l1 or l2:
rest = l1 if l1 else l2
while rest:
cur.next=rest
rest=rest.next
cur.next.next=None
cur=cur.next
return godnode.next