Skip to content

Commit edf0296

Browse files
committed
feat: Merge Two Sorted Lists
1 parent b4f2b30 commit edf0296

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

merge-two-sorted-lists/HodaeSsi.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
class Solution:
10+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
11+
if not list1:
12+
return list2
13+
if not list2:
14+
return list1
15+
16+
if list1.val < list2.val:
17+
list1.next = self.mergeTwoLists(list1.next, list2)
18+
return list1
19+
else:
20+
list2.next = self.mergeTwoLists(list1, list2.next)
21+
return list2
22+

0 commit comments

Comments
 (0)