如何在python中合并两个排序的链表[关闭]
Posted
技术标签:
【中文标题】如何在python中合并两个排序的链表[关闭]【英文标题】:how to merge two sorted linked lists in python [closed] 【发布时间】:2014-04-26 18:12:51 【问题描述】:我尝试了很多算法来合并链表...
def merge_lists(head1,head2):
if head1 is None and head2 is None:
return None
elif head1 is None:
return head2
elif head2 is None:
return head1
if head1.value <= head2.value:
result = head1
else:
result = head2
while head1 != None or head2 != None:
if head1 != None and head2 != None:
if head1.value <= head2.value:
result.next = head1
head1 = head1.next
else:
result.next = head2
head2 = head2.next
elif(head1!=None):
result.next = head1
elif(head2!=None):
result.next = head2
return result
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])
assert [1,10] == merge_lists([10], [1])
assert [1,2,4,5,6,7] == merge_lists([1,2,5], [4,6,7])
谁能给我通过这些测试用例的代码?提前致谢。
【问题讨论】:
发布您尝试过的代码(算法),并告诉我们您为什么认为它们不起作用。也许list#extend()
和sorted()
函数会有所帮助。
这些“链表”中的“链表”在哪里?
我希望 pass
实际上不是您尝试过的算法之一。
我们不会为你做作业。你甚至没有给我们足够的上下文让我们给你代码,即使我们想要。
"can any one give me the code to pass these test cases?"
- 我们是否应该相信您编写了这些测试用例,但甚至无法尝试实现相关算法??
【参考方案1】:
此操作只是在执行Merge Sort 的“合并”步骤,可以在O(l1+l2)
时间内完成。
一般的前提是同时迭代两个(已经排序的)列表,但只推进具有最低头值的列表,同时在结果输出中使用高级值。当两个源列表都用完时,操作完成。
这里有一些pseudo-code(由***提供),对于链表数据类型来说翻译起来应该不会太难。为链表实现它时,可以创建一个新列表,也可以破坏性地修改其中一个列表。
function merge(left, right)
// receive the left and right sublist as arguments.
// 'result' variable for the merged result of two sublists.
var list result
// assign the element of the sublists to 'result' variable until there is no element to merge.
while length(left) > 0 or length(right) > 0
if length(left) > 0 and length(right) > 0
// compare the first two element, which is the small one, of each two sublists.
if first(left) <= first(right)
// the small element is copied to 'result' variable.
// delete the copied one(a first element) in the sublist.
append first(left) to result
left = rest(left)
else
// same operation as the above(in the right sublist).
append first(right) to result
right = rest(right)
else if length(left) > 0
// copy all of remaining elements from the sublist to 'result' variable,
// when there is no more element to compare with.
append first(left) to result
left = rest(left)
else if length(right) > 0
// same operation as the above(in the right sublist).
append first(right) to result
right = rest(right)
end while
// return the result of the merged sublists(or completed one, finally).
// the length of the left and right sublists will grow bigger and bigger, after the next call of this function.
return result
【讨论】:
如何计算链表的长度? @srikarthikmodukurilength(l) > 0
表示“是一个非空链表”(例如,当前节点 l
是有效/非无)
@srikarthikmodukuri 顺便说一句,我刚刚看到的另一个问题中的代码,你永远不会更新循环中的 temp
节点,所以它永远不会真正“附加”。
根据你的算法我写了代码,但它进入了无限循环。你能帮我吗?提前致谢
@srikarthikmodukuri 无限循环是由temp
节点的突变引起的。对应的 l1/l2 节点的next
属性被设置之前它被遍历。【参考方案2】:
def merge_lists(x,y):
comb = []
comb += x + y
comb.sort()
return comb
【讨论】:
以上是关于如何在python中合并两个排序的链表[关闭]的主要内容,如果未能解决你的问题,请参考以下文章