Skip to content

Commit 2fc03ca

Browse files
committed
Add solution 021 [Python3]
1 parent da5e9b2 commit 2fc03ca

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#Definition for singly-linked list.
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution:
9+
def mergeTwoLists(self, l1, l2):
10+
"""
11+
:type l1: ListNode
12+
:type l2: ListNode
13+
:rtype: ListNode
14+
"""
15+
while l1 and l2:
16+
if l1.val < l2.val:
17+
l1.next=self.mergeTwoLists(l1.next,l2)
18+
return l1
19+
else:
20+
l2.next=self.mergeTwoLists(l1,l2.next)
21+
return l2
22+
return l1 or l2

0 commit comments

Comments
 (0)