越努力越幸运!
LinkedList
LinkedList 是双向链表。
- 链表批量增加,是靠for循环遍历原数组,依次执行插入节点操作。对比ArrayList是通过System.arraycopy完成批量增加的。增加一定会修改modCount。
通过下标获取某个node 的时候,(add select),会根据index处于前半段还是后半段 进行一个折半,以提升查询效率
删也一定会修改modCount。 按下标删,也是先根据index找到Node,然后去链表上unlink掉这个Node。 按元素删,会先去遍历链表寻找是否有该Node,如果有,去链表上unlink掉这个Node。
- 改也是先根据index找到Node,然后替换值。改不修改modCount。
- 查本身就是根据index找到Node。
- 所以它的CRUD操作里,都涉及到根据index去找到Node的操作。
一.LinkedList的特点
1.LinkedList
是线程不安全的,允许元素为null的双向链表。
其底层数据结构是链表,它实现List<E>, Deque<E>, Cloneable, java.io.Serializable
接口,它实现了Deque<E>
,所以它也可以作为一个双端队列。和ArrayList
比,没有实现RandomAccess
所以其以下标,随机访问元素速度较慢。2.因其底层数据结构是链表,所以可想而知,它的增删只需要移动指针即可,故时间效率较高。不需要批量扩容,也不需要预留空间,所以空间效率比
ArrayList
高。3.缺点就是需要随机访问元素时,时间效率很低,虽然底层在根据下标查询Node的时候,会根据index判断目标Node在前半段还是后半段,然后决定是顺序还是逆序查询,以提升时间效率。不过随着n的增大,总体时间效率依然很低。
4.当每次增、删时,都会修改modCount。
二.LinkedList的get操作
public int indexOf(Object o) {
int index = 0;
if (o == null) {//如果目标对象是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;
}
三LinkedList的add操作
1.增加单个结点
//在尾部插入一个节点: add
public boolean add(E e) {
linkLast(e);
return true;
}
//生成新节点 并插入到 链表尾部, 更新 last/first 节点。
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++;//修改size
modCount++;//修改modCount
}
//在指定下标,index处,插入一个节点
public void add(int index, E element) {
checkPositionIndex(index);//检查下标是否越界[0,size]
if (index == size)//在尾节点后插入
linkLast(element);
else//在中间插入
linkBefore(element, node(index));
}
//在succ节点前,插入一个新节点e
void linkBefore(E e, Node<E> succ) {
// assert succ != null;
//保存后置节点的前置节点
final Node<E> pred = succ.prev;
//以前置和后置节点和元素值e 构建一个新节点
final Node<E> newNode = new Node<>(pred, e, succ);
//新节点new是原节点succ的前置节点
succ.prev = newNode;
if (pred == null)//如果之前的前置节点是空,说明succ是原头结点。所以新节点是现在的头结点
first = newNode;
else//否则构建前置节点的后置节点为new
pred.next = newNode;
size++;//修改数量
modCount++;//修改modCount
}
2.批量增加
//addAll ,在尾部批量增加
public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);//以size为插入下标,插入集合c中所有元素
}
//以index为插入下标,插入集合c中所有元素
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);//检查越界 [0,size] 闭区间
Object[] a = c.toArray();//拿到目标集合数组
int numNew = a.length;//新增元素的数量
if (numNew == 0)//如果新增元素数量为0,则不增加,并返回false
return false;
Node<E> pred, succ; //index节点的前置节点,后置节点
if (index == size) { //在链表尾部追加数据
succ = null; //size节点(队尾)的后置节点一定是null
pred = last;//前置节点是队尾
} else {
succ = node(index);//取出index节点,作为后置节点
pred = succ.prev; //前置节点是,index节点的前一个节点
}
//链表批量增加,是靠for循环遍历原数组,依次执行插入节点操作。对比ArrayList是System.arraycopy完成批量增加的
for (Object o : a) {//遍历要添加的节点。
@SuppressWarnings("unchecked") E e = (E) o;
Node<E> newNode = new Node<>(pred, e, null);//以前置节点 和 元素值e,构建new一个新节点,
if (pred == null) //如果前置节点是空,说明是头结点
first = newNode;
else//否则 前置节点的后置节点设置问新节点
pred.next = newNode;
pred = newNode;//步进,当前的节点为前置节点了,为下次添加节点做准备
}
if (succ == null) {//循环结束后,判断,如果后置节点是null。 说明此时是在队尾append的。
last = pred; //则设置尾节点
} else {
pred.next = succ; // 否则是在队中插入的节点 ,更新前置节点 后置节点
succ.prev = pred; //更新后置节点的前置节点
}
size += numNew; // 修改数量size
modCount++; //修改modCount
return true;
}
//根据index 查询出Node,
Node<E> node(int index) {
// assert isElementIndex(index);
//通过下标获取某个node 的时候,(增、查 ),会根据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;
}
}
private void checkPositionIndex(int index) {
if (!isPositionIndex(index))
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private boolean isPositionIndex(int index) {
return index >= 0 && index <= size; //插入时的检查,下标可以是size [0,size]
}
小结:
- 链表批量增加,是靠for循环遍历原数组,依次执行插入节点操作。对比ArrayList是通过System.arraycopy完成批量增加的
- 通过下标获取某个node 的时候,(add select),会根据index处于前半段还是后半段 进行一个折半,以提升查询效率
四.LinkedList常见的面试题
1.ArrayList,List和LinkedList的区别
答:
1.List是接口类,ArrayList和LinkedList是List的实现类。
2.ArrayList是动态数组(顺序表)的数据结构。顺序表的存储地址是连续的,所以在查找比较快,但是在插入和删除时,由于需要把其它的元素顺序向后移动(或向前移动),所以比较耗时。
3.LinkedList是链表的数据结构。链表的存储地址是不连续的,每个存储地址通过指针指向,在查找时需要进行通过指针遍历元素,所以在查找时比较慢。由于链表插入时不需移动其它元素,所以在插入和删除时比较快。
2.LinkedList的读写效率
答:LinkedList 是链表的操作
get() 获取第几个元素,依次遍历,复杂度O(n)
add(E) 添加到末尾,复杂度O(1)
add(index, E) 添加第几个元素后,需要先查找到第几个元素,直接指针指向操作,复杂度O(n)
remove()删除元素,直接指针指向操作,复杂度O(1)