我在一些博客中读到新节点被添加到前面。但是当我检查linkedList
的源代码时,它在最后添加了node
并保持列表的head
地址不变,这应该是理想的行为。
public boolean add(E e) {
linkLast(e);
return true;
}
void linkLast(E e) {
final Node<E> l = last;
final Node<E> newNode = new Node<>(l, e, null);
last = newNode;
if (l == null)
first = newNode;
else
l.next = newNode;
size++;
modCount++;
}
单链表和双链表的节点添加行为有什么区别吗?
最佳答案
add(E e)
Appends the specified element to the end of this list.
(我的重点)
据我所知,java库中哪里没有单链表。您可以创建自己的。我认为唯一的区别在于:保存或不保存返回链接。
关于java - LinkedList 中的新元素添加到哪里?在头部之后还是在尾部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28062199/