合并链表

编程入门 行业动态 更新时间:2024-10-28 09:29:22
合并链表 - 不工作的python(Merge Linked List - Not working python)

有人能告诉我下面的代码有什么问题:

class Node(object): def __init__(self, data=None, next_node=None): self.data = data self.next = next_node class Solution: def MergeLists(self,headA, headB): current = Node() temp = current while not (headA is None and headB is None ): if(headA.data <= headB.data) : temp.next = headA headA = headA.next else: temp.next = headB headB = headB.next temp = temp.next if headA is None: temp.next = headB if headB is None: temp.next = headA return current.next

请让我知道我在这里做错了什么。 我正在学习python

Can someone tell me what is wrong with the below code:

class Node(object): def __init__(self, data=None, next_node=None): self.data = data self.next = next_node class Solution: def MergeLists(self,headA, headB): current = Node() temp = current while not (headA is None and headB is None ): if(headA.data <= headB.data) : temp.next = headA headA = headA.next else: temp.next = headB headB = headB.next temp = temp.next if headA is None: temp.next = headB if headB is None: temp.next = headA return current.next

Please let me know what I did wrong here. I am learning python

最满意答案

每次在循环中只修改temp变量,而不是尝试

if(headA.data<=headB.data): temp = headA headA = headA->next else: temp = headB headB = headB->next while not (headA is None and headB is None ): if(headA.data <= headB.data) : temp->next = headA headA = headA.next else: temp->next = headB headB = headB.next

Only the temp variable is modified each time in the loop, instead you could try

if(headA.data<=headB.data): temp = headA headA = headA->next else: temp = headB headB = headB->next while not (headA is None and headB is None ): if(headA.data <= headB.data) : temp->next = headA headA = headA.next else: temp->next = headB headB = headB.next

更多推荐

本文发布于:2023-07-30 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1321445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!