我刚开始编程,所以请原谅这个可能很愚蠢的问题。
我在做leetcode,我找到了链接列表。我想我理解它们,只是我不知道如何测试我的代码/调用我的函数(?)
这是我的代码,我知道它可以工作,因为我把它上传到leetcode,但是我仍然希望能够在我的机器上运行它。
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow我想我有两个不同的问题:
Solution = Solution() print(Solution.middleNode(head = [1,2,3,4,5,6]))但是,“头”不是一个正常的列表吗?我是否需要单独创建一个额外的函数来创建“链接”。每次您想要添加一个新节点时,我都看到通过调用一个函数来创建一个链接列表。那么,我会使用for循环来添加我的示例用例吗?
发布于 2022-07-11 11:34:50
如果你在找内部样板,下面是代码。
在这里,您需要为节点、链接列表和解决方案创建类。
然后,使用给定的数字,您需要创建一个linkedlist对象。上面的部分由他们自己用leetcode完成,这个对象被传递给类Solution方法middleNode,在那里OP代码运行并给出结果。接下来,一旦得到输出,就会与现有的解决方案进行比较。
# Node class for individual nodes
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# linked list class, to create a linked list of the list nodes
class LinkedList:
def __init__(self):
self.head = None
# adding a node element to linked list
def add(self, node):
if self.head is None:
self.head = node
else:
curr = self.head
while curr.next:
curr = curr.next
curr.next = node
curr = node
# printing element of existing linked list
def print_ll(self):
curr= self.head
while curr:
print(curr.val)
curr= curr.next
# leetcode solution class
class Solution:
def middleNode(self, head) :
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# value with which we create list nodes
values = [1,2,3,4,5,6,7,8,9,10]
ll = LinkedList() # create a linked list class object
for i in values:
node = ListNode(i) # creating a list node
ll.add(node) # adding list node to linked list
#ll.print_ll() # printing linked list
x = Solution().middleNode(ll.head) # passing linked list object to leetcode solution method and getting result
while x: # printing result
print(x.val)
x=x.next发布于 2022-07-11 11:29:39
要使您的代码在机器上工作,您必须实现两件事:首先,对于第一个答案,您必须实现在leetcode页面顶部给出的类ListNode:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next您从输入中导入“可选”:
from typing import Optional那么您就有了代码的先决条件。正如您所提到的,您必须初始化这个类。这里唯一的问题是,您的变量与类的名称相同,什么会在以后引起麻烦。最后,您必须像已经调用的那样调用函数,但有一个小的区别:必须以"head“作为ListNode类型的变量(而不是List )调用该函数,并返回一个类型为ListNode的变量。
简而言之,这将是我的解决方案(当然,您可以这样做,并尽可能多地使用ListNodes ):
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
# Initialise class
s = Solution()
# Defining nodes of the list, assigning always the next node.
seven = ListNode(7)
six = ListNode(6, next=seven)
five = ListNode(5, next=six)
four = ListNode(4, next=five)
three = ListNode(3, next=four)
two = ListNode(2, next=three)
one = ListNode(1, next=two)
# Calling your function (with "one" as your head node of the list)
# NOTE: As this gives you back an attribute of type ListNode, you have to access the "val" attribute of it to print out the value.
print(s.middleNode(one).val)https://stackoverflow.com/questions/72937506
复制相似问题