Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.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中的标准列表生成链表_Python_Linked List - Fatal编程技术网

如何从Python中的标准列表生成链表

如何从Python中的标准列表生成链表,python,linked-list,Python,Linked List,作为一个更大项目的一部分,我正在尝试从标准列表生成一个链接列表。我已经浏览了一些关于这个问题的主题,例如,但是大多数代码的体系结构都与我的链接列表本身有很大不同。只有最后一个答案与我的答案非常相似 我试图在这里完成的是创建一个生成器,其中包括一个函数,该函数从给定的输入创建链表,这就是为什么这里的结构是刚性的。我也不能触摸ListNode类 我尝试了以下代码,但它只返回以列表的最后一个元素为节点的单元素链表 我有一种感觉,我离得很近,但缺少了一些东西。如果需要,我可以创建助手函数,但理想情况下,

作为一个更大项目的一部分,我正在尝试从标准列表生成一个链接列表。我已经浏览了一些关于这个问题的主题,例如,但是大多数代码的体系结构都与我的链接列表本身有很大不同。只有最后一个答案与我的答案非常相似

我试图在这里完成的是创建一个生成器,其中包括一个函数,该函数从给定的输入创建链表,这就是为什么这里的结构是刚性的。我也不能触摸ListNode类

我尝试了以下代码,但它只返回以列表的最后一个元素为节点的单元素链表

我有一种感觉,我离得很近,但缺少了一些东西。如果需要,我可以创建助手函数,但理想情况下,我希望避免它。有人有什么想法吗?哪里错了

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Creator:

    def populate(self, in_list):
        # creating the head node
        out_list = ListNode(in_list[0])
        curr = out_list

        # iterating over input list
        for i in in_list[1:]:
            curr = curr.next
            curr = ListNode(i)

        return curr


# Below the manual approach for a list of four elements/nodes
# manual_list = ListNode(1)
# manual_list.next = ListNode(2)
# manual_list.next.next = ListNode(3)
# manual_list.next.next.next = ListNode(4)

inputs = [1,2,3,4]
result = Creator().populate(inputs)

while result:
    print(result.val)
    result = result.next

谢谢大家!

方向正确,只需在添加新节点后处理指针分配,同时保留对第一个节点的引用并返回:

def populate(self, in_list):
    # creating the head node
    curr = ListNode(in_list[0])
    head = curr
    # iterating over input list
    for i in in_list[1:]:
      temp = ListNode(i)
      curr.next = temp
      curr = temp

    return head
完整代码:

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Creator:

    def populate(self, in_list):
        # creating the head node
        curr = ListNode(in_list[0])
        head = curr
        # iterating over input list
        for i in in_list[1:]:
          temp = ListNode(i)
          curr.next = temp
          curr = temp

        return head


# Below the manual approach for a list of four elements/nodes
# manual_list = ListNode(1)
# manual_list.next = ListNode(2)
# manual_list.next.next = ListNode(3)
# manual_list.next.next.next = ListNode(4)

inputs = [1,2,3,4]
result = Creator().populate(inputs)

while result:
    print(result.val)
    result = result.next

我很乐意提供帮助,但大多数代码的体系结构都与我的不同,链表本身就是一个类。这是相当标准的,我认为这是最好的做法。但你到底为什么要使用链表呢?老实说,除了学校的练习之外,我永远也想象不出一个用python实现自己的链表的好用例。您几乎总是使用一个常规的列表对象,或者可能是一个collections.deque作为旁白,您的Creator类可能只是一个常规函数,它作为一个类没有任何用途……确切地说,这是一个处理非标准数据结构的教育项目的一部分。我从未在日常工作中使用过这些。