class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
if l1==None:
return l2
if l2 == None:
return l1
result = ListNode(0)
l=result
while 1:
if l1.val <= l2.val:
l.next = l1
l1=l1.next
if l1==None:
break
else:
l.next=l2
l2=l2.next
if l2 == None:
break
l=l.next
l.next.next=l1 or l2
return result.next
python leetcode 21. Merge Two Sorted Lists
最新推荐文章于 2024-04-03 21:24:25 发布
本文深入探讨了链表数据结构的合并算法,通过一个具体的Python类实现,展示了如何将两个有序链表合并为一个有序链表的过程。文章详细解释了算法的逻辑,包括处理链表节点比较、节点链接以及边界条件的判断。

1464

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



