LinkedList ,是基于节点实现的双向链表的 List ,每个节点都指向前一个和后一个节点从而形成链表。
LinkedList提供队列、双端队列、栈的功能。
LinkedList随机访问的平均时间复杂度为O(n),查找指定元素的平均时间复杂度为O(n)。
LinkedList移除指定位置的元素的最好时间复杂度是O(1),最坏时间复杂度是O(n),平均是O(n)。
LinkedList 添加元素的最好时间复杂度是O(1) ,最坏时间复杂度是O(n) ,平均时间复杂度是O(n)
LinkedList不仅可以在链头链尾添加删除元素,还可以在中间部分添加删除元素。只是要找到指定的元素需要遍历链表
实现了3个接口,其中如下三个与ArrayList一致:
transient int size = 0;//链表的大小
transient Node<E> first;//指向头结点的指针
transient Node<E> last;//指向尾结点的指针
public LinkedList() {
}
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}
因为没有容量一说,所以没有向ArrayList一样有一个参数为容量大小的构造函数。当然也就不存在扩容和缩容的问题。
添加元素的操作相对于ArrayList来说,不需要考虑容量的问题。
public boolean offerFirst(E e) {
addFirst(e);
return true;
}
addFirst(E e)
头插法,调用linkFirst(E e)
这个方法和上面提到的方法唯一的区别就在于一个有返回值一个没有返回值。
public void addFirst(E e) {
linkFirst(e);
}
push(E e)
调用addFirst(e);
其实和addFirst没有差别。只是为了提供一个常规的名字push
public void push(E e) {
addFirst(e);
}
private void linkFirst(E e) {
final Node<E> f = first;//指向头节点。
final Node<E> newNode = new Node<>(null, e, f);//根据传入的参数构建新的节点。前驱为null 后继为f。
first = newNode;//头指针first指向这个节点。
if (f == null)//若原先没有头节点
last = newNode;//则将尾指针也指向这个新的节点。
else //否则原先的头节点的前驱指向这个新的节点。
f.prev = newNode;
size++;
modCount++;
}
public boolean offerLast(E e) {
addLast(e);
return true;
}
public void addLast(E e) {
linkLast(e);
}
public boolean offer(E e) {
return add(e);
}
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++;
}
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
final Node<E> pred = succ.prev;
final Node<E> newNode = new Node<>(pred, e, succ);
succ.prev = newNode;
if (pred == null)
first = newNode;
else
pred.next = newNode;
size++;
modCount++;
}
public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
Node<E> node(int index) {
//找到索引index的节点。
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
说简单点就是当index等于链表长度的时候,将c集合中的元素一个接一个的添加到链尾。若index小于链表长度的时候,则找到索引index的节点。在这个节点之前插入集合c中的所有元素。
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);
Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
return false;
Node<E> pred, succ;
if (index == size) {
succ = null;
pred = last;
} else {
succ = node(index);
pred = succ.prev;
}
for (Object o : a) {
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);
if (pred == null)
first = newNode;
else
pred.next = newNode;
pred = newNode;
}
if (succ == null) {
last = pred;
} else {
pred.next = succ;
succ.prev = pred;
}
size += numNew;
modCount++;
return true;
}
public E remove() {
return removeFirst();
}
public E removeFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return unlinkFirst(f);
}
public E pop() {
return removeFirst();
}
private E unlinkFirst(Node<E> f) {
// assert f == first && f != null;
final E element = f.item; //头节点的元素
final Node<E> next = f.next;//头节点的后继
f.item = null;
f.next = null; // help GC
first = next;//更新头节点。
if (next == null)//若没有头节点了,则说明链表空了,尾指针也设置为空
last = null;
else
next.prev = null;//否则现在的头节点的前驱设为空。
size--;
modCount++;
return element;
}
public E removeLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return unlinkLast(l);
}
private E unlinkLast(Node<E> l) {
// assert l == last && l != null;
final E element = l.item;
final Node<E> prev = l.prev;
l.item = null;
l.prev = null; // help GC
last = prev;
if (prev == null)
first = null;
else
prev.next = null;
size--;
modCount++;
return element;
}
public boolean remove(Object o) {
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null) {
unlink(x);
return true;
}
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item)) {
unlink(x);
return true;
}
}
}
return false;
}
E unlink(Node<E> x) {
// assert x != null;
final E element = x.item;
final Node<E> next = x.next;
final Node<E> prev = x.prev;
if (prev == null) {
first = next;
} else {
prev.next = next;
x.prev = null;
}
if (next == null) {
last = prev;
} else {
next.prev = prev;
x.next = null;
}
x.item = null;
size--;
modCount++;
return element;
}
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));
}
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
public E element() {
return getFirst();
}
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
}
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
public boolean contains(Object o) {
return indexOf(o) != -1;
}
public int indexOf(Object o) {
int index = 0;
if (o == null) {
for (Node<E> x = first; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = first; x != null; x = x.next) {
if (o.equals(x.item))
return index;
index++;
}
}
return -1;
}
public int lastIndexOf(Object o) {
int index = size;
if (o == null) {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (x.item == null)
return index;
}
} else {
for (Node<E> x = last; x != null; x = x.prev) {
index--;
if (o.equals(x.item))
return index;
}
}
return -1;
}
public E peek() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekFirst() {
final Node<E> f = first;
return (f == null) ? null : f.item;
}
public E peekLast() {
final Node<E> l = last;
return (l == null) ? null : l.item;
}
public E pollLast() {
final Node<E> l = last;
return (l == null) ? null : unlinkLast(l);
}
public E poll() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
public E pollFirst() {
final Node<E> f = first;
return (f == null) ? null : unlinkFirst(f);
}
直接返回size属性
public int size() {
return size;
}
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out size
s.writeInt(size);
// Write out all elements in the proper order.
for (Node<E> x = first; x != null; x = x.next)
s.writeObject(x.item);
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i = 0; i < size; i++)
linkLast((E)s.readObject());
}
public Object clone() {
LinkedList<E> clone = superClone();
// Put clone into "virgin" state
clone.first = clone.last = null;
clone.size = 0;
clone.modCount = 0;
// Initialize clone with our elements
for (Node<E> x = first; x != null; x = x.next)
clone.add(x.item);
return clone;
}