Skip to content

Commit 2c6cfa5

Browse files
authored
Update merge_sorted_lists.py
1 parent 28370ed commit 2c6cfa5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

merge_sorted_lists.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,32 @@ def merge_lists(l1,l2):
2525
return sorted_list
2626

2727
print(merge_lists([1,2,20], [4,6,7,8]))
28+
29+
30+
###############OR ###########
31+
32+
def merge(a, b):
33+
"""
34+
a and b are two sorted lists
35+
"""
36+
i = 0
37+
j = 0
38+
c = []
39+
while i < len(a) and j < len(b):
40+
if a[i] < b[j]:
41+
c.append(a[i])
42+
i += 1
43+
else:
44+
c.append(b[j])
45+
j += 1
46+
if i < len(a):
47+
while i < len(a):
48+
c.append(a[i])
49+
i += 1
50+
if j < len(b):
51+
while j < len(b):
52+
c.append(b[j])
53+
j += 1
54+
return c
55+
56+
print(merge([1,2,20], [4,6,7,8]))

0 commit comments

Comments
 (0)