Java 如何删除链表中的第一个节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16507299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you remove the first Node in a Linked List?
提问by Raw415
Sup guys so I'm going over a few of my methods in my Linked List class and I'm getting a logical error when removing a node from a linked list. I was working on my removeFirst() method when I then encountered a error in my removeLast() method as well. The problem is that both remove the last item in the list. not sure why but here is my code.
伙计们,所以我将在我的链表类中回顾我的一些方法,当从链表中删除一个节点时,我遇到了一个逻辑错误。当我在 removeFirst() 方法中也遇到错误时,我正在处理我的 removeFirst() 方法。问题是两者都删除了列表中的最后一项。不知道为什么,但这是我的代码。
Remove First Node
删除第一个节点
public T removeFirst() throws EmptyCollectionException
{
// Checking to see if the List is empty or not
if ( isEmpty() )
throw new EmptyCollectionException("LinkedList");
Node < T > temp = contents;
T next = contents.getNext().getItem();
contents = new Node ( next, contents );
count--;
return temp.getItem();
}
Remove Last Node
删除最后一个节点
public T removeLast() // fixed
{
// Checking to see if the List is empty or not
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
// Node<T> temp = contents;
Node<T> current = contents;
Node<T> prev = null;
while (current.getNext() != null)
{
prev = current;
current = current.getNext();
}
prev.setNext(null);
count--;
return current.getItem();
}
I've looked around the questions already posted but I can't seem to find the answer I'm looking for.
I Know that a node has at least two values
one to hold the data and another to hold the reference to the next node
That's what I think is going on for the first one. But when I call the methods one after another they both get rid off the last node. Idk I will look over my code and update this question if necessary. But can you guys see where I'm going wrong and point me in the right direction. Thank you.
我环顾了已经发布的问题,但似乎找不到我正在寻找的答案。
我知道一个节点至少有两个值,
一个用来保存数据,另一个用来保存对下一个节点的引用。
这就是我认为第一个节点的情况。但是当我一个接一个地调用这些方法时,它们都摆脱了最后一个节点。Idk 我会查看我的代码并在必要时更新这个问题。但是你们能看到我哪里出错并指出我正确的方向吗?谢谢你。
采纳答案by ValarDohaeris
If you have a list A->B->C, A being the head ("contents") of your list, in order to remove it, you simply have to advance the pointer to B, i.e. the next node in your list:
如果您有一个列表 A->B->C,A 是列表的头部(“内容”),为了删除它,您只需将指针前移到 B,即列表中的下一个节点:
public T removeFirst() throws EmptyCollectionException {
// Checking to see if the List is empty or not
if ( isEmpty() )
throw new EmptyCollectionException("LinkedList");
Node<T> first = contents;
contents = contents.getNext();
count--;
return first.getItem();
}
Since you also need to return the data associated with the first node, you need to keep a temporary reference to it. (I called it first
)
由于您还需要返回与第一个节点关联的数据,因此您需要保留对它的临时引用。(我叫它first
)
回答by htkaya
I think you need to add head node to your liked list class for defining frst node of list.
我认为您需要将头节点添加到您喜欢的列表类中以定义列表的第一个节点。
public void deleteFront()
{
if (head!=null)
head = head.Next;
}
回答by gifpif
public void removeFirst() {
if (head == null)
return;
else {
if (head == tail) {
head = null;
tail = null;
} else {
head = head.next;
}
}
}
回答by Mahmud Hoque
public T removeFirst() throws EmptyCollectionException {
if (isEmpty())
throw new EmptyCollectionException("LinkedList");
Node < T > temp = contents;
T next = contents.getNext().getItem();
contents = new Node ( next, contents );
count--;
return temp.getItem();
}
In this method comment last three statement. then add below three lines
在此方法中注释最后三个语句。然后添加以下三行
contents=contents.getNext()
count--;
return next;
For removing last node: Its looks fine to me.
删除最后一个节点:对我来说它看起来不错。