-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0021.py
More file actions
34 lines (34 loc) · 923 Bytes
/
0021.py
File metadata and controls
34 lines (34 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
if not l1 and not l2:
return None
elif not l1:
head = l2
l2 = l2.next
elif not l2:
head = l1
l1 = l1.next
elif l1.val <= l2.val:
head = l1
l1 = l1.next
elif l2.val < l1.val:
head = l2
l2 = l2.next
node = head
while l2 and l1:
if l2.val <= l1.val:
node.next = l2
l2 = l2.next
else:
node.next = l1
l1 = l1.next
node = node.next
while l1:
node.next = l1
l1 = l1.next
node = node.next
while l2:
node.next = l2
l2 = l2.next
node = node.next
return head