-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path21MergeTwoSortedLists.py
More file actions
59 lines (58 loc) · 1.44 KB
/
Copy path21MergeTwoSortedLists.py
File metadata and controls
59 lines (58 loc) · 1.44 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def mergeTwoLists(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
a=ListNode(0)
l=a
if not l1 and not l2:
return a.next
if l1 and l2:
while l1 and l2:
if l1.val>=l2.val:
l.next=ListNode(l2.val)
l=l.next
l2=l2.next
else:
l.next=ListNode(l1.val)
l=l.next
l1=l1.next
if l1:
while l1:
l.next=ListNode(l1.val)
l=l.next
l1=l1.next
if l2:
while l2:
l.next=ListNode(l2.val)
l=l.next
l2=l2.next
return a.next
if __name__=='__main__':
arr1=[1,2,4]
arr2=[1,3,4]
p=ListNode(arr1[0])
l1=p
for i in arr1[1:]:
l1.next=ListNode(i)
l1=l1.next
l1=p
p=ListNode(arr2[0])
l2=p
for i in arr2[1:]:
l2.next=ListNode(i)
l2=l2.next
l2=p
solution=Solution()
result=solution.mergeTwoLists(l1,l2)
if result:
while result:
print(result.val)
result=result.next