Skip to content

Commit d7f5d8f

Browse files
committed
21
1 parent b8b7c86 commit d7f5d8f

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

merge-two-sorted-lists/jeldo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
# O(n+m), n = len(list1), m = len(list2)
3+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
4+
head = node = ListNode()
5+
while list1 and list2:
6+
if list1.val <= list2.val:
7+
node.next = list1
8+
list1 = list1.next
9+
else:
10+
node.next = list2
11+
list2 = list2.next
12+
node = node.next
13+
if list1:
14+
node.next = list1
15+
if list2:
16+
node.next = list2
17+
return head.next

0 commit comments

Comments
 (0)