【问题标题】:(Python) Summing two linked lists where each node is an integer. Attribute error(Python)对两个链表求和,其中每个节点都是一个整数。属性错误
【发布时间】:2021-07-07 11:41:06
【问题描述】:
class Node:
    def __init__(self, data = None, next = None):
        self.data = data
        self.next = next

class LinkedList:
    def __init__(self, node = None):
        self.head = node
        self.length = 0
    
    def InsertNode(self, data):
        newNode = Node()
        newNode.data = data
        if self.length == 0:
            self.head = newNode
        else:
            newNode.next = self.head
            self.head = newNode
        self.length += 1
    
    def printList(self):
        temp = self.head
        while temp != None:
            print(temp.data, end = " ")
            temp = temp.next

    

class AddingListNumbers:
    def addTwoNumbers(self, list1, list2):
        if list1 == None:
            return list2
        if list2 == None:
            return list1
        len1 = len2 = 0
        head = list1.head
        while head != None:
            len1 += 1
            head = head.next
        head = list2.head
        while head != None:
            len2 += 1
            head = head.next
        if len1 > len2:
            shorter = list2
            longer = list1
        else:
            shorter = list1
            longer = list2
        sum = None
        carry = 0
        while shorter != None:
            value = shorter.data + longer.data + carry
            carry = value / 10
            value -= carry * 10
            if sum == None:
                sum = Node(value)
                result = sum
            else:
                sum.next = Node(value)
                sum = sum.next
            shorter = shorter.next
            longer = longer.next
        while longer != None:
            value = longer.data + carry
            carry = value / 10
            value -= carry * 10
            sum.next = Node(value)
            sum = sum.next
            longer = longer.next
        if carry != 0:
            sum.next = Node(carry)
        return result

linkedlist = LinkedList()
linkedlist.InsertNode(19)
linkedlist.InsertNode(14)
linkedlist.InsertNode(11)
linkedlist.InsertNode(9)
linkedlist.InsertNode(6)
linkedlist.InsertNode(5)

linkedlist2 = LinkedList()
linkedlist2.InsertNode(17)
linkedlist2.InsertNode(16)
linkedlist2.InsertNode(13)
linkedlist2.InsertNode(6)
linkedlist2.InsertNode(2)
linkedlist2.InsertNode(1)
linkedlist2.InsertNode(24)
linkedlist2.InsertNode(3)
linkedlist2.InsertNode(11)

list3 = LinkedList()
ResultList = AddingListNumbers()
list3.next = ResultList.addTwoNumbers(linkedlist, linkedlist2)
list3.printList()

我创建了 Node 和 LinkedList 类,然后创建了另一个类添加列表编号来添加列表编号。 我收到一条错误消息:

值 = 较短的数据 + 较长的数据 + 进位 AttributeError: 'LinkedList' 对象没有属性 'data'

我不明白如何调试这个。如何处理属性错误?

Below is the image of the error message.

【问题讨论】:

    标签: python class linked-list attributeerror


    【解决方案1】:

    这个部分有问题:

    if len1 > len2:
        shorter = list2 # should be list2.head instead
        longer = list1 # should be list1.head instead
    else:
        shorter = list1 # should be list1.head instead
        longer = list2 # should be list2.head instead
    

    话虽如此,但可以优化此代码以仅遍历列表一次。在这个实现中,您首先找到较短的列表,然后在遍历较短的列表时添加。它可以在单次遍历中完成:

    1. 在遍历 list1 或完全遍历 list2 时重复这些步骤
    2. 从list1的节点添加数据,如果存在,或者添加0
    3. 从list2的节点添加数据,如果存在,或者添加0
    4. 将进位加到上述总和中。
    5. 计算总和的 divmod 并重新分配进位和总和。
    6. 如果存在下一个节点,则将指针向前移动。

    结果列表将包含列表的总和。

    【讨论】:

    • 做到了。谢谢兄弟。
    • @UMDowney 如果这对您有用,请将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多