JDK7中 LinkedList
private 方法 private void linkFirst(E e)
在新添加元素时链表不会断裂?
代码来源于 JDK7
private void linkFirst(E e) {
final Node<E> f = first;
final Node<E> newNode = new Node<>(null, e, f);
first = newNode;
if (f == null)
last = newNode;
else
f.prev = newNode;
// 此处没有执行 newNode.next = f; newNode.next 不会链接后面的元素
size++;
modCount++;
}
Why is the link broken?
f points to the original first and sets the new newNode to first.
At this time, it is judged that if the original linked list is empty, then last is also first.
If it is not empty, then the prev of f<original first node> points to first.
I guess the reason why you think the link is broken is because you didn't see that first's next points to f. Um, next has been passed into Node
final Node<E> newNode = new Node<>(null, e, f);
private static class Node<E> {