File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 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 list1 is None :
9+ return list2
10+ if list2 is None :
11+ return list1
12+
13+ if list1 .val < list2 .val :
14+ list1 .next = self .mergeTwoLists (list1 .next , list2 )
15+ return list1
16+ else :
17+ list2 .next = self .mergeTwoLists (list1 , list2 .next )
18+ return list2
19+
20+ # class Solution:
21+ # def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
22+ # result = ListNode(-1)
23+ # dummy = result
24+ # while list1 and list2:
25+ # if list1.val < list2.val:
26+ # dummy.next = list1
27+ # list1 = list1.next
28+ # else:
29+ # dummy.next = list2
30+ # list2 = list2.next
31+ # dummy = dummy.next
32+
33+ # if list1:
34+ # dummy.next = list1
35+ # else:
36+ # dummy.next = list2
37+
38+ # return result.next
You can’t perform that action at this time.
0 commit comments