合并链接列表 - 不工作python

时间:2015-10-14 01:34:07

标签: python linked-list

有人可以告诉我以下代码有什么问题:

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

1 个答案:

答案 0 :(得分:0)

每次在循环中只修改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