我试图从单个链接列表的末尾删除。我没有尾部变量来保持对列表中最后一项的引用。因此这是我的实现。我的问题是在while循环之后,如果我设置不起作用(它不会删除最后一个节点)。我必须设置current=null;。但是,接下来我要为current.next=null;添加什么。即使我说current = null,这并不意味着将当前节点指向null。有人可以解释一下为什么我必须在那使用next吗?public void removeFromTail() throws EmptyStackException{ if(head==null){ throw new EmptyStackException("can't delete .Empty list");} else if(head.next==null){ head=null; } else{ ListNode current=head; while(current.next.next!=null){ current=current.next; } current.next=null;}} 最佳答案 current是对链接列表中当前位置的引用。在while循环之后,current引用倒数第二个项目。说current.next = null时,使当前对象的next变为null。这使当前对象成为最后一个对象。当您说current = null时,您只是将本地引用变量设置为null。换句话说,它不再引用您的列表。它指的是null。关于java - 链接列表从尾部删除,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23675753/
10-10 17:09
查看更多