简体   繁体   English

删除链表中的最后一个节点

[英]Delete last node in linked list

I am implementing deletion of last node in linked list using Python.我正在使用 Python 实现链表中最后一个节点的删除。 Below is my code:下面是我的代码:

class Node:
    def __init__(self, key):
        self.key = key
        self.next = None
        
def printList(head):
    curr = head
    while curr != None:
        print(curr.key, end=" ")
        curr = curr.next
    
def deleteLastNode(head):
    if head == None:
        return None
    
    temp = head
    
    # Iterating till the last Node
    while temp.next != None:
        temp = temp.next
    temp = None
    return head

# Driver code
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

head = deleteLastNode(head)
printList(head)

However, in output I'm still getting the complete linked list.但是,在输出中我仍然得到完整的链表。

10 20 30

My doubt is why the last node is not deleted.我的疑问是为什么最后一个节点没有被删除。 Also, how is it printing value 30 when the last node is already set to temp = None ?此外,当最后一个节点已设置为temp = None时,它如何打印值30

Well, your linked list is:好吧,你的链表是:

10 -> 20 -> 30

^head

Your iteration:你的迭代:

10 -> 20 -> 30

^head       ^temp

Then when you do temp = None , just means(you just assign None to temp ):然后当你做temp = None ,只是意味着(你只是将None分配给temp ):

10 -> 20 -> 30       None

^head                ^temp

A correct way is when you iterate on 20 , do temp.next = None to remove the reference to the last node.正确的方法是当您迭代20 ,执行temp.next = None以删除对最后一个节点的引用。 So your code might be:所以你的代码可能是:

class Node:
    def __init__(self, key):
        self.key = key
        self.next = None
        
def printList(head):
    curr = head
    while curr != None:
        print(curr.key, end=" ")
        curr = curr.next
    
def deleteLastNode(head):
    if head == None:
        return None
    
    temp = head
    
    # Iterating till the last Node
    while temp.next.next != None:
        temp = temp.next
    temp.next = None
    return head

# Driver code
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

head = deleteLastNode(head)
printList(head)

This code would work when your linked list contain at least two elements.当您的链表包含至少两个元素时,此代码将起作用。 When there is only one element, this will raise exception.当只有一个元素时,这将引发异常。 I would recommend you use a dummy head node which next point to the real head node.我建议您使用一个虚拟头节点,它next指向真正的头节点。

#Another way of solving 
class Node:
    def __init__(self, key):
        self.key = key
        self.next = None
    
def printList(head):
    curr = head
    while curr != None:
        print(curr.key, end=" ")
        curr = curr.next

def deleteLastNode(head):
    if head == None:
        return None

    temp = head
    prev=None    #creating the value of previous element
    # Iterating till the last Node
    while temp.next != None:
        prev=temp  #updating for every iteration
        temp = temp.next
    prev.next = None #returning as NONE value
    return head

# Driver code
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

head = deleteLastNode(head)
printList(head)

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

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