# 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, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
dummy_node=ListNode(-1)
cur=dummy_node
l1=list1
l2=list2
while(l1 and l2):
if l1.val<=l2.val:
cur.next=l1
l1=l1.next
cur=cur.next
else:
cur.next=l2
l2=l2.next
cur=cur.next
if l1:
cur.next=l1
if l2:
cur.next=l2
return dummy_node.next
【LeetCode: 21. 合并两个有序链表 + 链表】
2024-05-01 09:00:02 14 阅读