Java - 在列表的末尾添加一个节点?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5790921/
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
Java - add a node to the end of a list?
提问by user618712
Here's what I have:
这是我所拥有的:
public class Node{
Object data;
Node next;
Node(Object data, Node next){
this.data = data;
this.next = next;
}
public Object getData(){
return data;
}
public void setData (Object data){
this.data = data;
}
public Node getNext(){
return next;
}
public void setNext(Node next){
this.next = next;
}
}
How do I write the code to add a Node at the end of a list?
如何编写代码以在列表末尾添加节点?
So if I had
所以如果我有
head -> [1] -> [2] -> null
How do I get to
我怎么去
head -> [1] -> [2] -> [3] -> null
Actually...I'm not even sure if I have to add to the end. I think it's valid to add and then sort? Not sure.
其实......我什至不确定我是否必须添加到最后。我认为添加然后排序是有效的吗?不确定。
Thanks!
谢谢!
回答by Sean Patrick Floyd
public void addToEnd(Object data){
Node temp = this;
while(temp.next!=null)temp=temp.next;
temp.next=new Node(data, null);
}
回答by Brian Roach
It's a linked list. You either have to
这是一个链表。你要么必须
A) iterate through all your nodes starting at the head, find the last node, then add one.
A)从头开始遍历所有节点,找到最后一个节点,然后添加一个。
or
或者
B) keep track of the tail, add to the tail, then update tail to the new last node.
B)跟踪尾部,添加到尾部,然后将尾部更新到新的最后一个节点。
回答by Petar Minchev
Start from the head:
从头开始:
Node currentNode = headNode;
while (node.getNext() != null) {
currentNode = currentNode.getNext();
}
currentNode.setNext(newNodeForInsertion);
A faster way is to store the last node of the list somewhere so you don't have to go through the whole list.
更快的方法是将列表的最后一个节点存储在某处,这样您就不必遍历整个列表。
回答by David
Recursively navigate through each node until you hit the end.
递归地浏览每个节点,直到到达终点。
public void navigate(Node insertNode)
{
if(next == null)
next = insertNode;
else
next.navigate(insertNode);
}
回答by Joseph Ottinger
To add to the end, you'd have to walk to the end of the list (i.e., to where next=null) and add a new node there.
要添加到末尾,您必须走到列表的末尾(即,到 next=null 的位置)并在那里添加一个新节点。
In the real world, you'd use an ArrayList for this and not bother with a linked list or manual structure at all.
在现实世界中,您会为此使用 ArrayList,而根本不用链表或手动结构。
回答by Edwin Buck
In your method to add a node, write a while loop that starts at the head and looks to see if the "next node" is null. If it is not, advance to the "next node" and repeat.
在添加节点的方法中,编写一个从头开始的 while 循环,并查看“下一个节点”是否为空。如果不是,则前进到“下一个节点”并重复。
Once you are at the node that points to nothing, adding the node is as simple as reassigning the null reference to the node to be added.
一旦您到达不指向任何内容的节点,添加节点就像将空引用重新分配给要添加的节点一样简单。
回答by Joeri Hendrickx
A recursive solution:
递归解决方案:
public void addToEnd(Object data){
if (next==null)
next = new Node(data, null);
else
next.addToEnd(data);
}
回答by tad604
Node n = head;
while(n.getNext() != null){
n = n.getNext();
}
n.setNext(nodeToAdd);
That's not sorted, which I can't tell from your question if you want it sorted. Which opens up another whole can of worms such as what do you want to sort on, if you have a linked list of type Object there isn't really anything meaningful to sort on.
这没有排序,如果你想要排序,我无法从你的问题中看出这一点。这打开了另一整罐蠕虫,例如您想对什么进行排序,如果您有一个 Object 类型的链接列表,那么实际上没有任何有意义的排序。