Merge Linked lists complete
This commit is contained in:
parent
57878d980d
commit
ebba53e376
21
NeetCodeRoadmap/LinkedLists/21_Merge_Two_Sorted_Lists.py
Normal file
21
NeetCodeRoadmap/LinkedLists/21_Merge_Two_Sorted_Lists.py
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user