-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21.py
More file actions
30 lines (30 loc) · 905 Bytes
/
21.py
File metadata and controls
30 lines (30 loc) · 905 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1 and not l2:
return None
dummy = ListNode
node = dummy
while l1 or l2:
if not l1:
node.next = l2
l2 = l2.next
node = node.next
elif not l2:
node.next = l1
l1 = l1.next
node = node.next
else:
if l1.val < l2.val:
node.next = l1
l1 = l1.next
node = node.next
else:
node.next= l2
l2 = l2.next
node = node.next
return dummy.next