在python中构造链接列表

在python中构造链接列表,python,Python,嘿,伙计们,我正在制作一个链接列表,我得到了这个错误 Traceback (most recent call last): File "python", line 144, in <module> File "python", line 93, in get AttributeError: 'NoneType' object has no attribute 'next' 回溯(最近一次呼叫最后一次): 文件“python”,第144行,在 get中第93行的文件“pyth

嘿,伙计们,我正在制作一个链接列表,我得到了这个错误

Traceback (most recent call last):
  File "python", line 144, in <module>
  File "python", line 93, in get
AttributeError: 'NoneType' object has no attribute 'next'
回溯(最近一次呼叫最后一次):
文件“python”,第144行,在
get中第93行的文件“python”
AttributeError:“非类型”对象没有属性“下一个”
这是否意味着我没有使用next,或者它可能是这里完全缺少的东西?或者我没有正确构建链接列表

class Node(object):

    def __init__(self,data):
        self.data = data
        self.next = None


class MyLinkedList(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """

        self.head = None
        self.size = 0

    def get(self, index):
        """
        Get the value of the index-th node in the linked list. If the 
index is invalid, return -1.
        :type index: int
        :rtype: int
        """
        if index <=  0 or index > self.size: #checks if index is less the 0 and chech if the index requested is bigger                                                  #then
            return -1                        #self.size
        else:
            current =  self.head
            for i in range(self.size):
                if i != index:
                current = current.next
                else:
                    return current.data

    def addAtHead(self, val):
        """
        Add a node of value val before the first element of the linked 
list. After the insertion, the new node will be the first node of the 
linked list.
        :type val: int
        :rtype: void
        """
        current = self.head
        node = Node(val)
        if current is None:
            current = node

        else:
            current.next = self.head
            self.head = node
        self.size = self.size + 1

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: void
        """
        tail = Node(val)
        if self.head is None:
            self.head = tail

        else:
            tmp = self.head
            while tmp is not None:
                tmp.next
            tmp = tail
类节点(对象):
定义初始化(自身,数据):
self.data=数据
self.next=无
类MyLinkedList(对象):
定义初始化(自):
"""
在这里初始化数据结构。
"""
self.head=无
self.size=0
def get(自我,索引):
"""
获取链接列表中第th个节点的索引值。如果
索引无效,返回-1。
:类型索引:int
:rtype:int
"""
if index self.size:#检查索引是否小于0,并检查请求的索引是否更大#然后
返回-1#自身大小
其他:
电流=自身磁头
对于范围内的i(自身尺寸):
如果我索引:
当前=当前。下一步
其他:
返回当前数据
def addAtHead(自身,val):
"""
在链接节点的第一个元素之前添加值为val的节点
插入后,新节点将成为
链表。
:type val:int
:rtype:void
"""
电流=自身磁头
节点=节点(val)
如果当前为无:
当前=节点
其他:
current.next=self.head
self.head=节点
self.size=self.size+1
def ADDATATAIL(自身,val):
"""
将值为val的节点附加到链表的最后一个元素。
:type val:int
:rtype:void
"""
尾部=节点(val)
如果self.head为无:
头=尾
其他:
tmp=自我头部
虽然tmp不是无:
下一个
tmp=尾部

你的缩进有点混乱,但是如果
当前
,你就不能执行
当前=当前。下一步
如果你在一个空列表上添加一个ad()
当前为无
),你就永远不会给列表分配一个新值。实际上,你的代码中有一些地方是错误的。AddAtTail不工作,AddAtHead在AddAtTail中head为None且大小未更新时引发该错误。不要返回-1以表示缺少元素;提出一个
LookupError