#21
Merge Two Sorted Lists
easy· Linked Listruns: 0You are given the heads of two sorted linked lists list1 and list2. Merge the two lists into a single sorted list by splicing together the nodes of the first two lists. Return the head of the merged linked list.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 490
class Solution:
def mergeTwoLists(
self, list1: ListNode | None, list2: ListNode | None
) -> ListNode | None:
dummy = ListNode()
tail = dummy
while list1 and list2:
if list1.val <= list2.val:
tail.next = list1
list1 = list1.next
else:
tail.next = list2
list2 = list2.next
tail = tail.next
tail.next = list1 or list2
return dummy.next
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.