合并链表 - 不工作的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.nextPlease 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.nextOnly 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更多推荐
发布评论