(Python) 对每个节点都是整数的两个链表求和。属性错误

(Python) Summing two linked lists where each node is an integer. Attribute error

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 classes,然后又创建了一个 class AddingListNumbers 用于添加列表编号。 我收到一条错误消息:

值=shorter.data+longer.data+进位 AttributeError: 'LinkedList' 对象没有属性 'data'

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

Below is the image of the error message.

这个部分有问题:

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. 如果存在下一个节点,则向前移动指针。

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