21. Merge Two Sorted Lists
list1
1
→2
→4
list2
1
→3
→4
1vs1
merged
∅
compare heads 1 and 1compare
1class Solution:2 def mergeTwoLists(self, list1, list2):3 head = current = None4 while list1 and list2:5 if list1.val <= list2.val:6 node = list17 list1 = list1.next8 else:9 node = list210 list2 = list2.next11 if head is None:12 head = current = node13 else:14 current.next = node15 current = current.next16 rest = list1 if list1 else list217 if head is None:18 head = rest19 else:20 current.next = rest21 return head