class Solution:
# @param {ListNode} l1
# @param {ListNode} l2
# @return {ListNode}
def mergeTwoLists(self, l1, l2):
head=ListNode(0)
temp = head
if l1 == None: return l2
if l2 == None: return l1
while l1 and l2:
if l1.val>l2.val:
temp.next = l2; l2 = l2.next; temp = temp.next
else:
temp.next = l1; l1 = l1.next; temp = temp.next
if l1 == None: temp.next = l2
if l2 == None: temp.next = l1
return head.nextLeetcode 21 Merge Two Sorted Lists
最新推荐文章于 2025-03-22 01:02:43 发布
本文介绍了一种将两个已排序的链表合并为一个排序链表的方法。通过定义一个辅助节点来跟踪当前节点,并比较两个输入链表的值,选择较小的节点连接到结果链表上,最终返回合并后的链表。
1463

被折叠的 条评论
为什么被折叠?



