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