LintCode Q165 Merge Two Sorted Lists in Python

  • Jinhai ZHOU
  • 3 Minutes
  • 2016年11月24日
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
"""
Definition of ListNode
class ListNode(object):
def __init__(self, val, next=None):
self.val = val
self.next = next
"""
class Solution:
"""
@param two ListNodes
@return a ListNode
"""
def mergeTwoLists(self, l1, l2):
# write your code here
dumy = ListNode(None)
temp = dumy
while l1 and l2:
if l1.val <= l2.val:
temp.next = l1
l1 = l1.next
else:
temp.next = l2
l2 = l2.next
temp = temp.next
if l1:
temp.next = l1
if l2:
temp.next = l2
return dumy.next
知识共享许可协议
本作品采用知识共享署名-相同方式共享 4.0 国际许可协议进行许可。