Skip to content

Commit 5b4fa96

Browse files
authored
Create Merge Sorted Lists.py
1 parent dfb968a commit 5b4fa96

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

interview_query/Merge Sorted Lists.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Given two sorted lists, write a function to merge them into one sorted list.
2+
3+
Bonus: Whats the time complexity?
4+
5+
def merge_list(t1, t2):
6+
7+
p1 = 0
8+
p2 = 0
9+
10+
res = []
11+
12+
while p1 < len(t1) and p2 < len(t2):
13+
14+
if t1[p1]< t2[p2]:
15+
res.append(t1[p1])
16+
p1+=1
17+
elif t1[p1]>= t2[p2]:
18+
res.append(t2[p2])
19+
p2+=1
20+
21+
22+
if p1 < len(t1):
23+
res.extend(t1[p1:])
24+
elif p2 < len(t2):
25+
res.extend(t2[p2:])
26+
return res

0 commit comments

Comments
 (0)