[英]Why can't I do a basic test on this basic linkedlist method I wrote in python?
Code below.代码如下。 The idea is to test if I'm printing the middle node of the linked list - and if the list has an even number of nodes, to return the first of the 2 middle nodes.这个想法是测试我是否正在打印链表的中间节点 - 如果列表有偶数个节点,则返回 2 个中间节点中的第一个。
However, this doesn't even run - it gives me a TypeError: middleNode() missing 1 required positional argument: 'head'然而,这甚至没有运行——它给了我一个 TypeError: middleNode() missing 1 required positional argument: 'head'
I'm giving it A as the head, and it's defined as a ListNode...so what am I missing?我把它作为头部,它被定义为一个 ListNode ......所以我错过了什么? Why isn't this working as intended?为什么这没有按预期工作?
class ListNode:
def __init__(self, val = 0, next = None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head):
slow = fast = head
if not fast.next:
return fast
if not fast.next.next:
return fast
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
if __name__ == "__main__":
A = ListNode(1)
B = ListNode(2)
C = ListNode(3)
A.next = B
B.next = C
C.next = ListNode(4)
C.next.next = ListNode(5)
C.next.next.next = ListNode(6)
print(A.next.val)
print(middleNode(A).val) #This is giving me an error
middleNode
is a method of Solution, but you never instanced Solution. middleNode
是一种解决方案的方法,但您从未实例化解决方案。 The entire last if statement is inside of your class.整个最后一个 if 语句在您的 class 内。 It needs to be moved to the left margin.它需要移动到左边距。 Those fixes will solve your problem, but you have other issues with being too verbose and spreading your code too thin.这些修复将解决您的问题,但您还有其他问题,即过于冗长和将代码传播得太细。 middleNode
should be a property of ListNode
, so you have access to it wherever you drag a ListNode
. middleNode
应该是ListNode
的一个属性,因此您可以在任何拖动ListNode
的地方访问它。
The problem with your version of middleNode
regards it's over-zealousness to determine what is not true, but then proceeded to check what is true.您的middleNode
版本的问题在于它过于热心地确定什么是不真实的,但随后继续检查什么是真实的。 All you have to do is automatically assume it isn't true and then check if it is.你所要做的就是自动假设它不是真的,然后检查它是否是真的。 If nothing was true then your assumption get's passed on.如果没有什么是真的,那么你的假设就会被传递。 Otherwise, whatever the truth is will be processed.否则,无论真相如何,都会被处理。 You also missed an "Aha,": that being: if there is a .next.next
then there is definitely also a .next
.你也错过了一个“啊哈”:那就是:如果有一个.next.next
那么肯定还有一个.next
。 By checking the former, the latter is implied.通过检查前者,后者是隐含的。
class ListNode:
def __init__(self, val = 0, next = None):
self.val = val
self.next = next
#Whereas this is a shorter way to write what you wrote
#I'm not convinced that this actually does what you want it to
#I'm pretty sure you will always get 3 nodes from the end
#Or in code terms the head of .next.next.next
#It only seems to work because you only count to 6
@property
def middleNode(self):
mid = self
head = self
while head.next.next:
mid = mid.next
head = head.next.next
return mid.val
if __name__ == "__main__":
A = ListNode(1)
B = ListNode(2)
C = ListNode(3)
A.next = B
B.next = C
C.next = ListNode(4)
C.next.next = ListNode(5)
C.next.next.next = ListNode(6)
print(A.next.val)
print(A.middleNode)
This cleans up your script, but I have to ask: Why not just use a list?这会清理您的脚本,但我不得不问:为什么不只使用列表? Something like:就像是:
nodes = [10,30,70,90,100,150,180]
print(nodes[int(len(nodes)/2)]) #virtually a middleNode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.