Skip to content

Commit 5f6d83e

Browse files
committed
21_merge-two-sorted-lists
1 parent 20baed8 commit 5f6d83e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

21_merge-two-sorted-lists.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
8+
if not list1: return list2
9+
elif not list2: return list1
10+
if list1.val < list2.val:
11+
list1.next = self.mergeTwoLists(list1.next, list2)
12+
return list1
13+
else:
14+
list2.next = self.mergeTwoLists(list1, list2.next)
15+
return list2

0 commit comments

Comments
 (0)