繁体   English   中英

如何删除链表中的节点?

[英]How do I remove node in Linked List?

我正在使用Python实现链表。 这是我的代码

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self, head = None):
        self.head = head

    def append(self, newElement):
        current = self.head
        if self.head:
            while current.next:
                current = current.next
            current.next = newElement
        else:
            self.head = newElement
    #get position time complexity O(n)
    #get the node at a specific position
    def get_position(self, position):
        current = self.head
        current_pos = 1
        while current_pos <= position:
            if current_pos == position:
                return current
            current = current.next
            current_pos += 1
        return None
    #Insert element
    # Time complexity O(n)
    def insert_element(self, element, position):
        if position > 1:
            front_pos = self.get_position(position - 1)
            end_pos = self.get_position(position + 1)
            front_pos.next = element
            element.next = end_pos
        else:
            element.next = self.head
            self.head = element
    def delete_element(self, element):
        current = self.head
        previous = None
        while current:
            if current.value == element:
                if not previous:
                    self.head = element
                previous.next = current.next

            else:
                current = current.next
                previous = current

        else:
            return False


node1 = Node("Iron Man")
node2 = Node("Capitain America")
node3 = Node("Doctor Strange")
node4 = Node("Spider man")
node5 = Node("Rieder")
print("Node1 Value is {}".format(node1.value))
print("Node1 next Value is {}".format(node1.next))
print("Node2 Value is {}".format(node2.value))
print("Node2 next Value is {}".format(node2.next))
Avengers = LinkedList()
Avengers.append(node1)
print("Firt element in link list is {}".format(Avengers.head.value))
Avengers.append(node2)
print("After Iron Man is {}".format(Avengers.head.next.value))
print(Avengers.get_position(2).value)
Avengers.append(node3)
Avengers.append(node4)
Avengers.insert_element(node5, 4)
print(Avengers.get_position(4).value)
Avengers.delete_element(node5)
print(Avengers.get_position(4).value)

这是我的输出:

Node1 Value is Iron Man
Node1 next Value is None
Node2 Value is Capitain America
Node2 next Value is None
Firt element in link list is Iron Man
After Iron Man is Capitain America
Capitain America
Rieder
Rieder

列表结构链接如下:Ironman-> Capitain America-> Strange Doctor-> Rieder-> Spiderman

因此,如果我不想使用“ Rieder”节点。 输出的最后一行应显示“ Spider man”

我的代码中发生了什么? 非常感谢帮助我的人:D

一个问题是您似乎要更新current ,然后用current的新值更新previous的。 我将回到您的delete方法,并逐行进行检查。 如果您在纸上写出列表的每一行的状态,并查看是否可以更正该算法,则将很有帮助。

else:
    current = current.next
    previous = current

首先,您需要修复您的insert_element函数。 您未正确插入节点。 这是正确的方法

def insert_element(self, element, position):
        if position > 1:
            front_pos = self.get_position(position - 1)
            # end_pos should not be position + 1, because it will
            # skip the element at index=position
            end_pos = self.get_position(position)
            front_pos.next = element
            element.next = end_pos
        else:
            element.next = self.head
            self.head = element

接下来,在删除元素时,只需要传递节点的值,而不传递节点本身。 我对您的代码做了一些小的更改,以修复一些错误。 查看评论以获取更多信息

def delete_element(self, element):
        current = self.head
        previous = None
        while current:
            if current.value == element:
                # This means that the head itself
                # has to be deleted
                if not previous:
                    self.head = self.head.next
                # Add an else block correctly here
                # in case head is not to be deleted
                else:
                    previous.next = current.next
                # break the loop when the element is deleted
                break

            else:
                # First copy the current into previous,
                # Then change the value of current
                previous = current
                current = current.next

同样也不需要else: return False在delete函数的末尾else: return False块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM