【发布时间】:2014-04-25 18:38:33
【问题描述】:
这是我的代码:
def merge_lists(head1, head2):
if head1 is None and head2 is None:
return None
if head1 is None:
return head2
if head2 is None:
return head1
if head1.value < head2.value:
temp = head1
else:
temp = head2
while head1 != None and head2 != None:
if head1.value < head2.value:
temp.next = head1
head1 = head1.next
else:
temp.next = head2
head2 = head2.next
if head1 is None:
temp.next = head2
else:
temp.next = head1
return temp
pass
这里的问题卡在无限循环中。谁能告诉我问题是什么
示例如下:
assert [] == merge_lists([],[])
assert [1,2,3] == merge_lists([1,2,3], [])
assert [1,2,3] == merge_lists([], [1,2,3])
assert [1,1,2,2,3,3,4,5] == merge_lists([1,2,3], [1,2,3,4,5])
【问题讨论】:
-
Python 原生列表成员没有
head和value属性。您的示例无法按原样运行。 -
我没明白你的意思你能告诉我更清楚吗@mtrw
-
@srikarthikmodukuri 我们不知道“head1”和“head2”指的是什么——您没有将它们包含在代码示例中。请做。
-
@srikarthikmodukuri - 如果
head1 = [1,2,3]然后访问head1.value将给出错误AttributeError: 'list' object has no attribute 'value'。因此,对于本机 python 列表,该程序将永远无法工作。您是否将一些不同类型的列表传递给函数merge_lists?? -
这里 head1 表示一个排序列表,head2 表示另一个排序列表@selllikesybok
标签: python list linked-list sorted