Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
两个链表之和python——来自CTCI的问题_Python - Fatal编程技术网

两个链表之和python——来自CTCI的问题

两个链表之和python——来自CTCI的问题,python,Python,我试图运行下面的代码,我希望代码返回一个名为head的列表,其中包含(第一个和第二个)的和,其中第一个和第二个是作为参数传递的链表 正如您在最后看到的,我创建了两个链表l1和l2。我假设这个链表将继承自节点类 但它给出了一个属性错误。我似乎想不出这个问题。 我是编程新手,自学成才。 是什么导致了这个错误?我们如何着手解决它 class Node: def __init__(self,data=None):

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

正如您在最后看到的,我创建了两个链表
l1
l2
。我假设这个链表将继承自
节点

但它给出了一个属性错误。我似乎想不出这个问题。 我是编程新手,自学成才。 是什么导致了这个错误?我们如何着手解决它

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'    

第一个是linkedList对象,因此它不能具有节点类的属性。如果要使第一个对象成为节点对象,则必须使用

First = Node()

如果要访问数据,则执行此操作将重新解决当前错误。如果您不明白我的意思,请告诉我

first_node = first.head
print(first_node.data)

您需要区分
链接列表
节点

我假设这个链表将继承自
节点

虽然他们有关系,但这不是继承。它们之间的唯一连接是
链接列表
包含
节点。继承在语法上是通过如下操作实现的:
类链接列表(节点):
,但在您的情况下,这在逻辑上似乎是不正确的(
链接列表
不是
节点
,但它确实包含
节点
,就像您当前的代码一样)

对变量使用有意义的名称有助于消除这种混淆。请注意,
addTwoList
的参数是列表,但您将它们视为具有
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)

我想您的意思是
首先是.head.data
first
是一个
链接列表
,它只有一个属性
。另一方面,
head
是具有
数据的
节点
对象attribute@Tomerikoo我已经编辑了代码,我是stackoverflow的新手,它给了我来自用户的反馈。不知道要遵循什么协议。谢谢,那么我的第一个评论呢?你试过了吗@Tomerikoo据我所知,这就是我所做的,carry+=first.head.data first=first.head.next,它给出了另一个错误,节点对象没有属性head。我在addTwoList方法中添加了first=Node()和second=Node(),现在它抛出错误carry+=first.data-TypeError:不支持的操作数类型对于+=:“int”和“NoneType”你是什么意思<代码>首先
是一个方法的参数…@Tomerikoo我就是这么说的,这是代码中的一个参数,但他们试图将其用作对象,他们必须将其定义为对象。虽然代码只能回答问题,但通过为代码提供上下文,您可以显著提高答案的质量,这段代码工作的原因,以及一些参考文档供进一步阅读。From:“简洁是可以接受的,但更全面的解释更好。”@SachinRajput我不明白你是什么saying@samanthaCannon请参阅此链接