如何修复链接列表的“在索引处添加”功能?

时间:2019-06-20 03:08:31

标签: python python-2.7 linked-list nodes

我正在处理Leetcode问题“设计链接列表”,并且在使我的addAtIndex部分无法正常工作方面遇到问题。有人可以看一眼,然后告诉我我在做什么错吗?

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

class MyLinkedList(object):

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

    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 >= self.getLength():
            return -1
        cur = self.head
        curIndex = 0
        while True:
            cur = cur.next
            if curIndex == index:
                return cur.data
            curIndex += 1

    def getLength(self):
        cur = self.head
        total = 0
        while cur.next != None:
            total += 1
            cur=cur.next
        return total

    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: None
    """
        first = node(val)
        first.next = self.head
        self.head = first

    def addAtTail(self, val):
    """
    Append a node of value val to the last element of the linked list.
    :type val: int
    :rtype: None
    """
        last = node(val)
        cur = self.head
        while cur.next != None:
            cur = cur.next
        cur.next = last

    def addAtIndex(self, index, val):
    """
    Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
    :type index: int
    :type val: int
    :rtype: None
    """
        newNode = node(val)
        cur = self.head
        curIndex = 0
        prev = None

        if index == self.getLength()-1:
            return self.addAtTail(val)
        elif index >= self.getLength():
            return
        if index == 0:
            newNode.next = self.head
            self.head = newNode
        else:
            while curIndex < index:
                prev = cur
                cur = cur.next
                curIndex = curIndex+1
            newNode.next = prev.next
            prev.next = newNode
            cur = newNode


    def deleteAtIndex(self, index):
    """
    Delete the index-th node in the linked list, if the index is valid.
    :type index: int
    :rtype: None
    """
    #if it's not the head or tail node
        cur = self.head
        curIndex = 0
        if (cur.next!= None) and (self.head != None) and (index != 0):
            while (cur.next != None):
                if curIndex == index-1:
                    cur.next = (cur.next).next
                curIndex += 1
                cur = cur.next
    #if it's the head node
        elif index == 0:
            temp = self.head
            self.head = temp.next
    #if it's the tail node
        else:
            temp = self.head
            while temp.next != None:
                prev = temp
                temp = temp.next
            prev.next = None

我希望输出[“ MyLinkedList”,“ addAtHead”,“ addAtTail”,“ addAtIndex”,“ get”,“ deleteAtIndex”,“ get”] [[],[1],[3],[1,2],[1],[1],[1]] 成为 [null,null,null,null,2,null,3], 但我不断 [null,null,null,null,3,null,2]。

我做错了什么?

0 个答案:

没有答案