python - 两个链表之和 python --CTCI 的问题

标签 python

我正在尝试运行下面的代码,我希望代码返回一个名为 head 的列表,其中包含(第一个和第二个)的总和,其中第一个和第二个是作为参数传递的链接列表。

正如你在最后看到的,我创建了两个链接列表l1l2。我假设这个链表将从 Node 类继承。

但是它给出了属性错误。我似乎无法弄清楚问题所在。 我是编程新手,自学者。 可能是什么原因导致此错误?我们该如何解决这个问题?

class Node:                                  
    def __init__(self,data=None):            
        self.data = data                     
        self.next = None                     
                                             
class LinkedList:                            
    def __init__(self):                      
        self.head = None                     
                                             
    def display(self):                       
        elems = []                           
        current = self.head                  
        while current != None:               
            elems.append(current.data)       
            current = current.next           
        return elems                         
                                             
    def append(self, data):                  
        elem = Node(data)                    
        if self.head == None:                
            self.head = elem                 
        else:                                
            current = self.head              
            while current.next != None:      
                current = current.next       
            current.next = elem              
                                             
    def addTwoLists(self, first, second):    
        head = third = Node(0)               
        carry = 0                            
                                             
        while first or second or carry:      
            if first:                        
                carry += first.data          
                first = first.next           
            if second:                       
                carry += second.data         
                second = second.next         
                                             
            third.data = carry % 10          
            carry = carry // 10              
                                             
            if first or second or carry:     
                third.next = Node(0)         
                third = third.next           
        return head
                             
ll = LinkedList()             
list1 = LinkedList()          
list2 = LinkedList()          
list1.append(7)               
list1.append(1)               
list1.append(6)               
print(list1.display())        
list2.append(5)               
list2.append(9)               
list2.append(2)               
print(list2.display())        
ll.addTwoLists(list1,list2)   
print(ll.display())    

我得到的错误是:

  carry += first.data
AttributeError: 'LinkedList' object has no attribute 'data'    

最佳答案

您需要区分 LinkedListNode

I am assuming this linked lists will inherit from the Node class.

虽然它们有关系,但它不是继承。它们之间的唯一联系是 LinkedList 包含 Node。继承是通过执行类似以下操作在语法上实现的: class LinkedList(Node):,但这在您的情况下似乎在逻辑上不正确(LinkedList is not Node,但它确实包含一个Node,就像您当前的代码一样)。

对变量使用有意义的名称可以帮助解决这种困惑。请注意,addTwoLists 的参数是列表,但您将它们视为具有 data 属性的节点。

您需要决定的另一件事是您的函数是否返回一个新列表或改变一个列表。我知道您想返回一个新方法,在这种情况下,该方法可以是静态的,也可以根本不在类内部(因为您从未真正使用过 self )。目前,您的方法不会改变任何对象并且您也不使用其返回值。

您可以在类外部定义它并使其返回一个新列表:

def addTwoLists(first_list, second_list):
    third_list = LinkedList()
    carry = 0

    first_node = first_list.head
    second_node = second_list.head
    while first_node or second_node or carry:      
        if first_node:                        
            carry += first_node.data          
            first_node = first_node.next           
        if second_node:                       
            carry += second_node.data         
            second_node = second_node.next         
                                         
        third_list.append(carry % 10)
        carry = carry // 10              
                                                   
    return third_list

然后在您的主代码中,只需执行 ll = addTwoLists(list1, list2)

关于python - 两个链表之和 python --CTCI 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64700356/

相关文章:

python - Keras 模型的矩阵大小错误

Python:为什么列表元素在使用程序后仍然没有消失?

python - async_generator block

Python 具有特定位数的数字范围

python - Tkinter Checkbutton 不会改变我的变量

python - json 树中超出了最大递归深度

python - Pygame,操纵杆检测不起作用

Python:继承类函数(属性 getter )不起作用

python - 使用 Python 编辑动画 gif 标题循环值

python - Numpy 聚合平均值