1. ArrayList的缺陷
ArrayList底层使用数组来存储元素,由于其底层是一段连续空间,当在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。
因此:java集合中又引入了LinkedList,即链表结构。
2. 链表
2.1 链表的概念及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的。
实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
- 单向或者双向
- 带头或者不带头
- 循环或者非循环
重点掌握两种:
- 无头单向非循环链表:结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。
- 无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。
2.2 链表的实现
public class MySingleList {
static class ListNode {
public int val; // 节点的值域
public ListNode next; // 下一个节点的地址
public ListNode(int val) {
this.val = val;
}
}
public ListNode head; // 当前链表的头节点
public ListNode creatList() {
ListNode node1 = new ListNode(12);
ListNode node2 = new ListNode(23);
ListNode node3 = new ListNode(34);
ListNode node4 = new ListNode(45);
node1.next = node2;
node2.next = node3;
node3.next = node4;
this.head = node1;
return head;
}
public void display() {
ListNode cur = head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
node.next = head;
head = node;
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = head;
if (cur == null) {
head = node;
} else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
if (index < 0 || index > size()) {
System.out.println("index位置不合法!");
return;
}
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
ListNode cur = findIndexSubOne(index);
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
// 找到index位置
private ListNode findIndexSubOne(int index) {
ListNode cur = head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
private ListNode searchPrev(int key) {
ListNode cur = head;
while (cur.next != null) {
if (cur.next.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if (head == null) {
return;
}
if (head.val == key) {
head = head.next;
return;
}
/* ListNode cur = head;
while (cur.next != null) {
if (cur.next.val == key) {
break;
}
cur = cur.next;
}*/
ListNode cur = searchPrev(key);
ListNode del = cur.next;
cur.next = del.next;
}
//删除所有值为key的节点
public void removeAllKey(int key) {
if (this.head == null) {
return;
}
ListNode cur = this.head.next;
ListNode prev = this.head;
while (cur != null) {
if (cur.val == key) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
if (this.head.val == key) {
head = head.next;
}
}
public void clear() {
// this.head = null;
ListNode cur = this.head;
ListNode curNext = null;
while (cur != null) {
curNext = cur.next;
cur.next = null;
cur = curNext;
}
head = null;
}
}
3. LinkedList的模拟实现
public class IndexOutOfException extends RuntimeException {
public IndexOutOfException() {
}
public IndexOutOfException(String message) {
super(message);
}
}
public class MyLinkedList {
static class ListNode {
private int val;
private ListNode prev;
private ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;
public ListNode last;
public void display() {
ListNode cur = head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//得到单链表的长度
public int size() {
int count = 0;
ListNode cur = head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = head;
while (cur != null) {
if (cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//头插法
public void addFirst(int data) {
ListNode node = new ListNode(data);
if (head == null) {
head = node;
last = node;
} else {
node.next = head;
head.prev = node;
head = node;
}
}
//尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
if (head == null) {
head = node;
last = node;
} else {
last.next = node;
node.prev = last;
last = node;
}
}
private boolean checkIndex(int index) {
if (index < 0 || index > size()) {
throw new IndexOutOfException("index不合法!");
}
return true;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
checkIndex(index);
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
ListNode node = new ListNode(data);
/* ListNode cur = head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
node.next = cur.next;
node.prev = cur;
cur.next.prev = node;
cur.next = node;*/
ListNode cur = searchIndex(index);
node.next = cur;
node.prev = cur.prev;
cur.prev.next = node;
cur.prev = node;
}
private ListNode searchIndex(int index) {
ListNode cur = head;
while (index != 0) {
cur = cur.next;
index--;
}
return cur;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
ListNode cur = head;
while (cur != null) {
if (cur.val == key) {
if (cur == head) {
// 头节点
head = head.next;
if (head != null) {
head.prev = null;
} else {
last = null;
}
} else {
if (cur.next != null) {
// 中间节点
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
} else {
// 尾节点
// cur.prev.next = cur.next;
cur.prev.next = null;
last = last.prev;
}
}
return;
} else {
cur = cur.next;
}
}
}
//删除所有值为key的节点
public void removeAllKey(int key) {
ListNode cur = head;
while (cur != null) {
if (cur.val == key) {
if (cur == head) {
// 头节点
head = head.next;
if (head != null) {
head.prev = null;
} else {
last = null;
}
} else {
if (cur.next != null) {
// 中间节点
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
} else {
// 尾节点
// cur.prev.next = cur.next;
cur.prev.next = null;
last = last.prev;
}
}
cur = cur.next;
} else {
cur = cur.next;
}
}
}
public void clear() {
ListNode cur = head;
while (cur != null) {
ListNode curNext = cur.next;
cur.prev = null;
cur.next = null;
cur = curNext;
}
head = null;
last = null;
}
}
4. LinkedList的使用
4.1 什么是LinkedList
LinkedList 的官方文档
LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。
在集合框架中,LinkedList也实现了List接口,具体如下:
【说明】:
- LinkedList实现了List接口;
- LinkedList的底层使用了双向链表;
- LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问;
- LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1);
- LinkedList比较适合任意位置插入的场景。
4.2 LinkedList的使用
- LinkedList的构造
方法 | 解释 |
---|---|
LinkedList() | 无参构造 |
public LinkedList(Collection<? extends E> c) | 使用其他集合容器中元素构造List |
public static void main(String[] args) {
// 方法一:
List<Integer> list1 = new LinkedList<>();
// 方法二:
List<Integer> list2 = new LinkedList<>();
list2.add(1);
list2.add(2);
list2.add(3);
// 方法三
List<Integer> list3 = new LinkedList<>(list2);
}
- LinkedList的其他常用方法介绍
方法 | 解释 |
---|---|
boolean add(E e) | 尾插 e |
void add(int index, E element) | 将 e 插入到 index 位置 |
boolean addAll(Collection<? extends E> c) | 尾插 c 中的元素 |
E remove(int index) | 删除 index 位置元素 |
boolean remove(Object o) | 删除遇到的第一个 o |
E get(int index) | 获取下标 index 位置元素 |
E set(int index, E element) | 将下标 index 位置元素设置为 element |
void clear() | 清空 |
boolean contains(Object o) | 判断 o 是否在线性表中 |
int indexOf(Object o) | 返回第一个 o 所在下标 |
int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
List subList(int fromIndex, int toIndex) | 截取部分 list |
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
// 1. add(elem): 表示尾插
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size()); // 7
System.out.println(list); // [1, 2, 3, 4, 5, 6, 7]
System.out.println("----------------------");
// 2. add(index, elem): 在index位置插入元素elem
// 在起始位置插入0
list.add(0, 0);
System.out.println(list); // [0, 1, 2, 3, 4, 5, 6, 7]
System.out.println("----------------------");
// 3. remove(): 删除第一个元素,内部调用的是removeFirst()
list.remove();
System.out.println(list); // [1, 2, 3, 4, 5, 6, 7]
list.removeFirst(); // removeFirst(): 删除第一个元素
System.out.println(list); // [2, 3, 4, 5, 6, 7]
list.removeLast(); // removeLast(): 删除最后元素
System.out.println(list); // [2, 3, 4, 5, 6]
list.remove(1); // remove(index): 删除index位置的元素
System.out.println(list); // [2, 4, 5, 6]
System.out.println("----------------------");
// 4. contains(elem): 检测elem元素是否存在,如果存在返回true,否则返回false
if(!list.contains(1)){
list.add(0, 1);
}
list.add(1);
System.out.println(list); // [1, 2, 4, 5, 6, 1]
System.out.println("----------------------");
// 5. indexOf(elem): 从前往后找到第一个elem的位置
System.out.println(list.indexOf(1)); // 0
// lastIndexOf(elem): 从后往前找第一个1的位置
System.out.println(list.lastIndexOf(1)); // 5
System.out.println("----------------------");
// 6.1 get(index): 获取指定位置元素
// 6.2 set(index, elem): 将index位置的元素设置为elem
int elem = list.get(0);
list.set(0, 100);
System.out.println(list); // [100, 2, 4, 5, 6, 1]
System.out.println("----------------------");
// 7. subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
List<Integer> copy = list.subList(0, 3);
System.out.println(list); // [100, 2, 4, 5, 6, 1]
System.out.println(copy); // [100, 2, 4]
System.out.println("----------------------");
// 8. clear(): 将list中元素清空
list.clear();
System.out.println(list.size()); // 0
}
- LinkedList的遍历
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(1); // add(elem): 表示尾插
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
System.out.println(list.size()); // 7
// 1. foreach遍历
for (int e : list) {
System.out.print(e + " "); // 1 2 3 4 5 6 7
}
System.out.println();
// 2. 使用迭代器遍历---正向遍历
ListIterator<Integer> it = list.listIterator();
while (it.hasNext()) {
System.out.print(it.next() + " "); // 1 2 3 4 5 6 7
}
System.out.println();
// 3. 使用反向迭代器---反向遍历
ListIterator<Integer> rit = list.listIterator(list.size());
while (rit.hasPrevious()) {
System.out.print(rit.previous() + " "); // 7 6 5 4 3 2 1
}
System.out.println();
}
5. ArrayList和LinkedList的区别
不同点 | ArrayList | LinkedList |
---|---|---|
存储空间上 | 物理上一定连续 | 逻辑上连续,但物理上不一定连续 |
随机访问 | 支持O(1) | 不支持:O(N) |
头插 | 需要搬移元素,效率低O(N) | 只需修改引用的指向,时间复杂度为O(1) |
插入 | 空间不够时需要扩容 | 没有容量的概念 |
应用场景 | 元素高效存储+频繁访问 | 任意位置插入和删除频繁 |