-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeTwoLists.py
More file actions
39 lines (32 loc) · 851 Bytes
/
MergeTwoLists.py
File metadata and controls
39 lines (32 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 15 20:58:22 2025
@author: hadis
"""
class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""
i , j = 0 , 0
merged = []
while i < len(list1) and j < len(list2):
if list1[i]<list2[j]:
merged.append(list1[i])
i+=1
else:
merged.append(list2[j])
j+=1
while i<len(list1):
merged.append(list1[i])
i+=1
while j<len(list2):
merged.append(list2[j])
j+=1
return merged
nums1 = [1, 2, 4]
nums2 = [1, 3, 4]
sol = Solution()
print(sol.mergeTwoLists(nums1, nums2))