简体   繁体   English

删除单个链接列表的最后一个节点

[英]Deleting the last node of a singly linkedlist

Below is the function to delete the last node of a singly linked list. 下面是删除单链表的最后一个节点的功能。 I don't understand, why are we creating a temp Node? 我不明白,为什么我们要创建一个临时节点? I tried doing it without the temp Node and used the node itself but the output doesn't delete the last node. 我尝试在没有临时节点的情况下执行此操作并使用节点本身,但输出不会删除最后一个节点。 Also, since we are using the temp Node, why are we returning the node and not temp? 此外,由于我们使用临时节点,为什么我们返回节点而不是临时节点? We aren't making any changes to node, so how is node affected? 我们没有对节点进行任何更改,那么节点如何受到影响?

public Node deleteLastNode(Node node)
{
    if (node.next == null || node == null)
        return null;
    Node temp = node;

    while (temp.next.next != null)
    {
        temp = temp.next;
    }
    temp.next = null;
    return node;
}

The reason why temp node is usually used is that the node is the head/starting of the list, which is the only representation of the list we have (by definition of a linked list). 通常使用temp节点的原因是node是列表的头部/起始点,这是我们所拥有的列表的唯一表示(通过链接列表的定义)。 Hence we do not want to alter the head(or representation of our list) and that is the reason node is returned from the method - which means we are returning the updated list after deletion is performed. 因此,我们不想改变头部(或列表的表示),这就是从方法返回node的原因 - 这意味着我们在执行删除后返回更新的列表。

First of all you need to switch this to conditions like this 首先,你需要将其切换到这样的条件

if (node.next == null || node == null) to 
if (node == null || node.next == null) 

this may cause null pointer exception. 这可能会导致空指针异常。 next..I think that temp is need to hold data before assigning null so that the real reference would not loose data. next ..我认为temp需要在赋值之前保存数据,以便真正的引用不会丢失数据。

I don't understand why we are creating a temp Node? 我不明白为什么我们要创建一个临时节点?

That's because you're storing current iteration node in that temp variable. 那是因为你将当前的迭代节点存储在那个temp变量中。

I tried doing it without the temp Node and used the node itself but the output doesn't delete the last node. 我尝试在没有临时节点的情况下执行此操作并使用节点本身,但输出不会删除最后一个节点。

Code needed to provide any feedback. 代码需要提供任何反馈。

Also, since we are using the temp Node, why are we returning node and not temp? 另外,既然我们正在使用临时节点,为什么我们要返回节点而不是临时节点?

Because you're returning reference to the head of the list, it just doesn't have last element anymore. 因为您正在返回对列表头部的引用,所以它不再具有最后一个元素。

We aren't making any changes to node which so how is node getting affected? 我们没有对节点进行任何更改,因此节点如何受到影响?

You're deleting last node here temp.next = null; 你在这里删除最后一个节点temp.next = null;

Hope it makes things a little bit clear for you. 希望它能让你有点清楚。

To navigate linked list to its last node, you need a pointer (cursor) pointing at a node you are supposing to be last pending the test this.next == null . 要将链表导航到其最后一个节点,您需要一个指针(光标)指向您认为最后一个待测试的this.next == null

Without the temporary node (aka cursor, or pointer) how could you interact with any node in the list? 没有临时节点(也就是游标或指针),您如何与列表中的任何节点进行交互?

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

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