Skip to content

Commit 041ac85

Browse files
21 ready
1 parent 19b3423 commit 041ac85

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Diff for: 21 Merge Two Sorted Lists/solution.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 21. Merge Two Sorted Lists
2+
# You are given the heads of two sorted linked lists list1 and list2.
3+
#
4+
# Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
5+
#
6+
# Return the head of the merged linked list.
7+
#
8+
# Input: list1 = [1,2,4], list2 = [1,3,4]
9+
# Output: [1,1,2,3,4,4]
10+
# Example 2:
11+
#
12+
# Input: list1 = [], list2 = []
13+
# Output: []
14+
# Example 3:
15+
#
16+
# Input: list1 = [], list2 = [0]
17+
# Output: [0]
18+
#
19+
20+
list1, list2 = [1, 2, 4], [1, 3, 4]
21+
22+
23+
def merge2list(list1, list2) -> list:
24+
final_list = []
25+
po1, po2 = 0, 0
26+
itter = 0
27+
lenght = (len(list1) + len(list2))
28+
29+
while itter < lenght:
30+
if po1 == len(list1):
31+
final_list.append(list2[po2])
32+
po2 += 1
33+
itter += 1
34+
elif po2 == len(list2):
35+
final_list.append(list1[po1])
36+
po1 += 1
37+
itter += 1
38+
39+
elif list1[po1] <= list2[po2]:
40+
final_list.append(list1[po1])
41+
po1 += 1
42+
itter += 1
43+
else:
44+
if list1[po1] > list2[po2]:
45+
final_list.append(list2[po2])
46+
po2 += 1
47+
itter += 1
48+
49+
return final_list
50+
51+
52+
print(merge2list(list1, list2))

Diff for: practice_small_code.ipynb

+24
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@
102102
}
103103
],
104104
"execution_count": 1
105+
},
106+
{
107+
"metadata": {
108+
"ExecuteTime": {
109+
"end_time": "2024-08-28T00:41:13.233348Z",
110+
"start_time": "2024-08-28T00:41:13.226317Z"
111+
}
112+
},
113+
"cell_type": "code",
114+
"source": [
115+
"list1 = [1,2,4]\n",
116+
"print(len(list1))"
117+
],
118+
"id": "d5b84c5825e2c378",
119+
"outputs": [
120+
{
121+
"name": "stdout",
122+
"output_type": "stream",
123+
"text": [
124+
"3\n"
125+
]
126+
}
127+
],
128+
"execution_count": 1
105129
}
106130
],
107131
"metadata": {

0 commit comments

Comments
 (0)