JDK7中LinkedList
private 方法private void linkFirst(E e)
在新添加元素时链表不会断裂?
代码来源于 JDK7
private void linkFirst(E e) { final Node f = first; final Node 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? points to first.
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
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 newNode = new Node<>(null, e, f); {
final Node
private static class Node