简体   繁体   English

如何从Java工作中的单链表中删除尾部

[英]How does remove tail from singly Linked List in Java work

I am reading algorithm to delete last element of a singly Linked List. 我正在阅读算法以删除单个链接列表的最后一个元素。 Assume I have a Linked List Object called ListNode: 假设我有一个名为ListNode的链接列表对象:

public class ListNode {
    private int data;
    private ListNode next;

    public ListNode(int data) {
        this.data = data;
    }

    public int getData() {
        return this.data;
    }

    public void setData(int data) {
        this.data = data;
    }

    public ListNode getNext() {
        return this.next;
    }

    public void setNext(ListNode next) {
        this.next = next;
    }
}

I found the method to delete the last node of the list is: 我发现删除列表最后一个节点的方法是:

public ListNode deleteAtTail(ListNode head) {

    if (head == null || head.next == null) return null;
    ListNode node = head;
    while(node.next.next != null) {
        node = node.next;
    }

    node.next = null;
    return head;
}

I am confusing how this code is working since everything is through "node". 我很困惑这个代码是如何工作的,因为一切都通过“节点”。 However, when return head, the last node is deleted. 但是,当返回头时,最后一个节点被删除。 Therefore, I wonder how it is working, is it related to "passed by value" in Java? 因此,我想知道它是如何工作的,它与Java中的“通过值传递”有关吗?

You iterate over the node s of the list until node.next.next is null. 您遍历列表的node s,直到node.next.next为null。 At this point, node refers to the next to last node, and node.next refers to the last node. 此时, node指的是倒数第二个节点, node.next指的是最后一个节点。 Setting node.next to null removes the last node from the list, since no node in the list refers to it anymore. node.next设置为null将从列表中删除最后一个节点,因为列表中的任何节点都不再引用它。

You can notice that the method is iterating through all nodes till second last node because the next of last node will be null . 你可以看到,该方法是通过直到倒数第二个节点的所有节点进行迭代,因为next最后一个节点将是null

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

Above code will give you second last node and its next is set to null using node.next = null; 上面的代码将会给你倒数第二个节点,其下设置为null使用node.next = null; This means second last node will become last node now. 这意味着第二个节点现在将成为最后一个节点。

Since none of the answers are clear (nor correct in my opinion, because if we have node.next.next and we have only 1 element, we will get a NullPointerException), I'd like to give my two cents. 因为没有一个答案是明确的(在我看来也不正确,因为如果我们有node.next.next并且我们只有1个元素,我们将得到一个NullPointerException),我想给我两分钱。

There are 3 scenarios: 有3种情况:

  1. List is empty. 列表是空的。 Straightforward, return null, or print that list is empty. 直截了当,返回null,或打印该列表为空。
  2. List has 1 item. 列表有1个项目。 We can't know this without counting (or can we?), but look at the code below 我们无法知道这一点(或者我们可以吗?),但请看下面的代码
  3. List has more than one item. 列表有多个项目。 Go through the list and have a temp variable for the previous, then set previous.next as null when you get to the end. 浏览列表并为前一个设置临时变量,然后在结束时将previous.next设置为null。

So, my approach is to have an initial previous variable set as null (imagine you are before the beginning of the list). 所以,我的方法是将一个初始的前一个变量设置为null(假设你在列表的开头之前)。 Then in the while loop, if it has 1 item, it wont execute any commands (it will skip it and prev will be null), else, go with point 3. Here's the code: 然后在while循环中,如果它有1个项目,它将不执行任何命令(它将跳过它并且prev将为null),否则,转到第3点。这是代码:

    if(head == null) return;
    ListNode iterator = head;
    ListNode prev = null;
    while(iterator.next !=null) {
        prev = iterator;
        iterator=iterator.next;
    }
    if(prev == null) head = null;
    else prev.next = null;

Hope this helps. 希望这可以帮助。

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

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