Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.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 为什么链接列表的代码在HackerRank中显示错误?_Python_Data Structures_Linked List - Fatal编程技术网

Python 为什么链接列表的代码在HackerRank中显示错误?

Python 为什么链接列表的代码在HackerRank中显示错误?,python,data-structures,linked-list,Python,Data Structures,Linked List,我已经用空闲的代码实现了链表。如果我遍历它,它将显示预期的输出。但在hackerrank我有麻烦了。我错过了什么? 问题就在这里 python中的getter和setter是冗余的。而且,你把事情搞得太复杂了 你只需要担心两种情况;当head为None时,一般情况和拐角情况 解决方案1 迭代 解决方案2 递归 这两种解决方案都通过了Hackerrank上的所有测试用例 在您的代码中self.tail.set\u next(新节点)显示错误,因为set\u next是类节点的函数您可以通过节点对

我已经用空闲的代码实现了链表。如果我遍历它,它将显示预期的输出。但在hackerrank我有麻烦了。我错过了什么? 问题就在这里


python中的getter和setter是冗余的。而且,你把事情搞得太复杂了

你只需要担心两种情况;当
head
None
时,一般情况和拐角情况

解决方案1
迭代


解决方案2
递归


这两种解决方案都通过了Hackerrank上的所有测试用例

在您的代码中
self.tail.set\u next(新节点)
显示错误,因为
set\u next
是类
节点的函数
您可以通过
节点对象

特里蒂斯

class Node:

   def __init__(self,data,nextNode=None):
       self.data = data
       self.nextNode = nextNode

   def getData(self):
       return self.data

   def setData(self,val):
       self.data = val

   def getNextNode(self):
       return self.nextNode

   def setNextNode(self,val):
       self.nextNode = val

class LinkedList:

   def __init__(self,head = None):
       self.head = head
       self.size = 0

   def getSize(self):
       return self.size

   def addNode(self,data):
       newNode = Node(data,self.head)
       self.head = newNode
       self.size+=1
       return True

   def printNode(self):
       curr = self.head
       while curr:
           print(curr.data)
           curr = curr.getNextNode()

myList = LinkedList()
print("Inserting")
print(myList.addNode(5))
print(myList.addNode(15))
print(myList.addNode(25))
print("Printing")
myList.printNode()
print("Size")
print(myList.getSize())
其数据结构如下所示

最后6行没有任何意义。您希望head和tail是什么样子的?我认为您应该使用示例代码中为您的语言提供的Node类。测试代码似乎依赖于某些属性的存在,例如
节点。接下来
插入函数在哪里?按要求返回链接列表的标题在哪里?程序的第一行应该是
def Insert(node,data):
、no
class node
和no
class LL
,并且该函数应该返回一些内容。即使是一个简单的问题,也不可能提供解决方案,不了解需要什么。Hackerrank应该对此进行评估,但您未能“使用以下(插入)方法返回列表的标题”,谢谢您的回答。事实上,在看到讨论之后,我已经尝试过这样做。但是,可能是我的理论知识让我困惑!如果我以这种方式在空闲状态下编写代码,如何遍历列表?@carl坚持问题中的节点定义。从那里开始。此代码适用于Hackerrank中的骨架代码。首先要理解它在做什么,一旦你完全理解了正在发生的事情,然后再去玩弄它。@carl对了,还有一件事。如果答案是有用的,请考虑投票表决。谢谢
def Insert(head, data):
    # handle the corner case
    if not head:
        return Node(data)

    # handle the general case
    temp = head
    while temp.next:
        temp = temp.next
    temp.next = Node(data)

    return head
def Insert(head, data):
    if not head:
        return Node(data)

    head.next = Insert(head.next, data)
    return head
class Node:

   def __init__(self,data,nextNode=None):
       self.data = data
       self.nextNode = nextNode

   def getData(self):
       return self.data

   def setData(self,val):
       self.data = val

   def getNextNode(self):
       return self.nextNode

   def setNextNode(self,val):
       self.nextNode = val

class LinkedList:

   def __init__(self,head = None):
       self.head = head
       self.size = 0

   def getSize(self):
       return self.size

   def addNode(self,data):
       newNode = Node(data,self.head)
       self.head = newNode
       self.size+=1
       return True

   def printNode(self):
       curr = self.head
       while curr:
           print(curr.data)
           curr = curr.getNextNode()

myList = LinkedList()
print("Inserting")
print(myList.addNode(5))
print(myList.addNode(15))
print(myList.addNode(25))
print("Printing")
myList.printNode()
print("Size")
print(myList.getSize())