-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path21.py
180 lines (156 loc) · 4.83 KB
/
21.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
__________________________________________________________________________________________________
40ms
# 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(-1)
cur = head
while l1 != None and l2 != None:
if l1.val < l2.val:
cur.next = l1
l1 = l1.next
else:
cur.next = l2
l2 = l2.next
cur = cur.next
if l1 == None:
cur.next = l2
if l2 == None:
cur.next = l1
return head.next
__________________________________________________________________________________________________
44ms
# 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:
if l1 is None:
return l2
elif l2 is None:
return l1
if l1.val <= l2.val:
head = l1
l1 = l1.next
else:
head = l2
l2 = l2.next
head.next = l1
prio = head
while l1 is not None and l2 is not None:
if l1.val <= l2.val:
l1 = l1.next
prio = prio.next
else:
prio.next = l2
prio = prio.next
l2 = l2.next
prio.next = l1
if l1 is None:
prio.next = l2
del prio
return head
__________________________________________________________________________________________________
48ms
class Solution:
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
if not l1 and not l2:
return None
if not l1:
return l2
if not l2:
return l1
if l1.val <= l2.val:
first_node = ListNode(l1.val)
l1 = l1.next
else:
first_node = ListNode(l2.val)
l2 = l2.next
cur_node = first_node
while l1 and l2:
if l1.val <= l2.val:
cur_node.next = ListNode(l1.val)
cur_node = cur_node.next
l1 = l1.next
else:
cur_node.next = ListNode(l2.val)
cur_node = cur_node.next
l2 = l2.next
while l1:
print(l1.val)
cur_node.next = ListNode(l1.val)
cur_node = cur_node.next
l1 = l1.next
while l2:
print(l2.val)
cur_node.next = ListNode(l2.val)
cur_node = cur_node.next
l2 = l2.next
return first_node
__________________________________________________________________________________________________
12144 kb
# 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':
l_ret = None
n = l_ret
while l1 or l2:
if not l2 or (l1 is not None and l1.val <= l2.val):
val = l1.val
l1 = l1.next
else:
val = l2.val
l2 = l2.next
if l_ret is None:
l_ret = ListNode(val)
n = l_ret
else:
n.next = ListNode(val)
n = n.next
return l_ret
__________________________________________________________________________________________________
12172 kb
# 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':
if not l1:
return l2
if not l2:
return l1
if l1.val <= l2.val:
head = l1
l1 = l1.next
else:
head = l2
l2 = l2.next
head_copy = head
while l1 or l2:
if not l1:
head.next = l2
return head_copy
if not l2:
head.next = l1
return head_copy
if l1.val <= l2.val:
head.next = l1
head = head.next
l1 = l1.next
else:
head.next = l2
head = head.next
l2 = l2.next
__________________________________________________________________________________________________