首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用链接列表

问调用链接列表
EN

Stack Overflow用户
提问于 2022-07-11 11:02:40
回答 2查看 136关注 0票数 -1

我刚开始编程,所以请原谅这个可能很愚蠢的问题。

我在做leetcode,我找到了链接列表。我想我理解它们,只是我不知道如何测试我的代码/调用我的函数(?)

我正在处理的问题

这是我的代码,我知道它可以工作,因为我把它上传到leetcode,但是我仍然希望能够在我的机器上运行它。

代码语言:javascript
复制
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

我想我有两个不同的问题:

  • "OptionalListNode) -> OptionalListNode:“部分
  • 以及函数的实际调用
  1. 之前的一些问题使用了像"List“这样的”输入“模块函数,所以我只需导入它们,它们就不会成为一个问题。但我不知道该怎么做
  2. 为了检查我的解决方案,我编写了一小部分代码,可以将示例输入放入其中。 Solution = Solution() print(Solution.middleNode(head = [1,2,3,4,5,6]))

但是,“头”不是一个正常的列表吗?我是否需要单独创建一个额外的函数来创建“链接”。每次您想要添加一个新节点时,我都看到通过调用一个函数来创建一个链接列表。那么,我会使用for循环来添加我的示例用例吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-11 11:34:50

如果你在找内部样板,下面是代码。

在这里,您需要为节点、链接列表和解决方案创建类。

然后,使用给定的数字,您需要创建一个linkedlist对象。上面的部分由他们自己用leetcode完成,这个对象被传递给类Solution方法middleNode,在那里OP代码运行并给出结果。接下来,一旦得到输出,就会与现有的解决方案进行比较。

代码语言:javascript
复制
# 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
票数 0
EN

Stack Overflow用户

发布于 2022-07-11 11:29:39

要使您的代码在机器上工作,您必须实现两件事:首先,对于第一个答案,您必须实现在leetcode页面顶部给出的类ListNode:

代码语言:javascript
复制
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

您从输入中导入“可选”:

代码语言:javascript
复制
from typing import Optional

那么您就有了代码的先决条件。正如您所提到的,您必须初始化这个类。这里唯一的问题是,您的变量与类的名称相同,什么会在以后引起麻烦。最后,您必须像已经调用的那样调用函数,但有一个小的区别:必须以"head“作为ListNode类型的变量(而不是List )调用该函数,并返回一个类型为ListNode的变量。

简而言之,这将是我的解决方案(当然,您可以这样做,并尽可能多地使用ListNodes ):

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72937506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档