Skip to content

Commit f056ea5

Browse files
committed
solution: merge-two-sorted-lists
1 parent 7e53ec1 commit f056ea5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

merge-two-sorted-lists/wozlsla.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
# Intuition
3+
-
4+
# Approach
5+
-
6+
"""
7+
8+
# Definition for singly-linked list.
9+
# class ListNode:
10+
# def __init__(self, val=0, next=None):
11+
# self.val = val
12+
# self.next = next
13+
14+
15+
class Solution:
16+
def mergeTwoLists(
17+
self, list1: Optional[ListNode], list2: Optional[ListNode]
18+
) -> Optional[ListNode]:
19+
20+
if not (list1 and list2):
21+
return list1 or list2
22+
23+
if list1.val < list2.val:
24+
list1.next = self.mergeTwoLists(list1.next, list2)
25+
return list1
26+
else:
27+
list2.next = self.mergeTwoLists(list1, list2.next)
28+
return list2

0 commit comments

Comments
 (0)