Merge Linked lists complete

This commit is contained in:
Jacob Delgado 2024-09-30 21:12:19 -07:00
parent 57878d980d
commit ebba53e376

View File

@ -0,0 +1,21 @@
def mergeTwoLists(l1, l2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
dummy = ListNode()
tail = dummy
while l1 and l2:
if l1.val < l2.val:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next
if l1:
tail.next = l1
elif l2:
tail.next = l2
return dummy.next