Skip to content

Commit ae67756

Browse files
committed
Week 4: merge-two-sorted-lists solution
1 parent c538722 commit ae67756

File tree

1 file changed

+16
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)