def merge_sorted(list1,list2):
if len(list1) == 0 and len(list2) != 0:
return list2
if len(list2) == 0 and len(list1) != 0:
return list1
if len(list1) == 0 and len(list2) == 0:
return []
if list1 is None and len(list2) != 0:
return list2
if list2 is None and len(list1) != 0:
return list1
if list1 is None and list2 is None:
return None
merged_list = [None] * (len(list1) + len(list2))
list1_pointer = 0
list2_pointer = 0
index = 0
while list1_pointer < len(list1) and list2_pointer < len(list2):
if list1[list1_pointer] < list2[list2_pointer]:
merged_list[index] = list1[list1_pointer]
index += 1
list1_pointer += 1
else:
merged_list[index] = list2[list2_pointer]
index += 1
list2_pointer += 1
while list1_pointer < len(list1):
merged_list[index] = list1[list1_pointer]
index += 1
list1_pointer += 1
while list2_pointer < len(list2):
merged_list[index] = list2[list2_pointer]
index += 1
list2_pointer += 1
return merged_list
arr1 = [2,4,5,8,9]
arr2 = [1,2,3,4,5,6,7]
print(merge_sorted(arr1,arr2))