如何从Python中的标准列表生成链接列表 [英] How to generate a linked-list from a standard list in Python

查看:34
本文介绍了如何从Python中的标准列表生成链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为一个更大的项目的一部分,我试图从标准列表中生成一个链接列表.我已经在SO上浏览了有关此问题的一些主题(例如,此处),但是大多数代码的体系结构与我的不同(链表本身是一个类).只有最后一个答案与我的解决方案非常相似.

As part of a bigger project, I'm trying to generate a linked-list from a standard list. I've already looked through some topics regarding this problem on SO (e.g. here) but most of codes have much different architecture than mine (linked-list is a class itself). Only the last answer was very similar to my solution.

我在这里试图完成的工作是创建一个生成器,该生成器除其他功能外,还具有根据给定输入创建链表的功能(这就是为什么结构很僵硬的原因).我也不能碰 ListNode 类.

What I try to accomplish here is to create a generator having, among others, a function creating linked-list from a given input (that's why the structure is rigid here). Also I can't touch ListNode class.

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

I've tried the following code but it returns only single element linked-list with the last element of the list as a node.

我有种接近的感觉,但是缺少了一些东西.如果需要,我可以创建辅助函数,但理想情况下,我想避免它.有没有人有什么想法?哪里错了?

I have a feeling I am close but something is missing. I can create helper functions if that's required but ideally, I would like to avoid it. Has anyone any ideas? Where's the mistake?

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

谢谢!

推荐答案

您的方向正确,添加新节点后只需照顾指针分配,还保留对第一个节点的引用并返回:

You are in the right direction, just take care of the pointer allocation after adding new node, also keep a reference to the first node and return that:

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中的标准列表生成链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆