文章目录
一、ArrList的缺陷
由于其底层是一段连续空间,当在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。因此:java集合中又引入了LinkedList,即链表结构
二、链表
(一)链表的概念及结构
1.定义
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链表次序实现的
注意:
- 链式结构在逻辑上是连续的,但是在物理上不一定连续
- 现实中的节点一般都是从堆上申请出来的
- 从堆上申请的空间,是按照一定的策略分配的,两次申请的空间可能连续,也可能不连续
实际中链表的结构非常多,主要是由于下面几种情况的考虑:
- 单向或者双向
- 带头结点或者不带头节点
- 循环或者非循环
我们主要学习无头单向非循环链表和无头双向链表
2.单链表的模拟实现
class MySingleList {
/**
* 节点内部类
*/
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//不初始化了 默认就是null
/**
* 打印链表里面的数据
*/
public void display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val+" ");
cur = cur.next;
}
System.out.println();
}
/**
* 得到单链表的长度
* @return 返回长度
*/
public int size(){
int size = 0;
ListNode cur = head;
while (cur != null) {
size++;
cur = cur.next;
}
return size;
}
//头插法
public void addFirst(int data){
ListNode newNode = new ListNode(data);
newNode.next = head;
head = newNode;
}
//尾插法
public void addLast(int data){
ListNode temp = new ListNode(data);
if(head == null) {
head = temp;
return;
}
ListNode cur = head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = temp;
}
/**
* 任意位置插入,第一个数据节点为0号下标
* @param index 插入的坐标
* @param data 插入节点的val值
*/
public boolean addIndex(int index,int data){
if(index == 0) {
addFirst(data);
return true;
}
try {
if(index > size()){
throw new InsertOutBoundException();
}
}catch (InsertOutBoundException e) {
e.printStackTrace();
}
ListNode cur = this.head;
ListNode newNode = new ListNode(data);
while(index - 1 != 0) {
cur = cur.next;
index--;
}
newNode.next = cur.next;
cur.next = newNode;
return true;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
if(this.head == null) {
return false;
}
ListNode cur = this.head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字为key的节点
public void remove(int key){
if(this.head == null) {
return;
}
if(head.val == key) {
head = head.next;
return;
}
ListNode cur = this.head;
while(cur.next != null) {
if(cur.next.val == key) {
cur.next = cur.next.next;
return;
}
cur = cur.next;
}
}
//删除所有值为key的节点
public void removeAllKey(int key){
while(this.head != null && this.head.val == key) {
this.head = this.head.next;
}
if(this.head == null) {
return;
}
ListNode prev = this.head;
ListNode cur = this.head.next;
while(cur != null) {
if(cur.val == key) {
prev.next = cur.next;
} else {
prev = cur;
}
cur = cur.next;
}
}
public void clear() {
head = null;
}
}
三、LinkedList的模拟实现
LinkedList底层就是一个双向链表
链表的头节点的pre == null,尾节点的next == null
class MyLinkedList {
static class ListNode {
public int val;
public ListNode prev;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//标记双向链表的头部
public ListNode tail;//标记双向链表的尾部
//头插法
public void addFirst(int data){
ListNode newNode = new ListNode(data);
if(this.head == null) {
this.head = this.tail = newNode;
return;
}
this.head.prev = newNode;
newNode.next = this.head;
this.head = newNode;
}
//尾插法
public void addLast(int data){
if(this.head == null) {
this.head = this.tail = new ListNode(data);
} else {
this.tail.next = new ListNode(data);
tail.next.prev = tail;
tail = tail.next;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data){
//1、判断Index位置的合法性
int length = size();
if(index < 0 || index > length) {
return;
}
//2、判断特殊位置,头插 和 尾插
if(head == null || index == 0) {
addFirst(data);
return;
}
if(index == length) {
addLast(data);
return;
}
//3、找到index位置节点的地址
ListNode cur = findIndexListNode(index);
ListNode node = new ListNode(data);
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
node.next = cur;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key) {
ListNode cur = this.head;
while(cur != null) {
if(cur.val == key) {
return true;
}
cur = cur.next;
}
return false;
}
private ListNode findIndexListNode(int index) {
if(head == null) {
return null;
}
ListNode cur = head;
while(index > 0 && cur != null) {
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(this.head != null) {
head.prev = null;
}else {
tail = null;
}
}else {
cur.prev.next = cur.next;
if(cur.next != null) {
cur.next.prev = cur.prev;
}else {
this.tail = cur.prev;
}
}
return;
}
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(this.head != null) {
head.prev = null;
}else {
tail = null;
}
}else {
cur.prev.next = cur.next;
if(cur.next != null) {
cur.next.prev = cur.prev;
}else {
this.tail = cur.prev;
}
}
}
cur = cur.next;
}
}
//得到单链表的长度
public int size() {
ListNode cur = this.head;
int size = 0;
while(cur != null) {
size++;
cur = cur.next;
}
return size;
}
public void display() {
ListNode cur = this.head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public void clear() {
ListNode cur = this.head;
while(cur != null) {
ListNode curNext = cur.next;
cur.next = null;
cur.prev = null;
cur = curNext;
}
this.head = null;
this.tail = null;
}
}
四、LinkedList的使用
1. 什么是LinkedList
LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高
【说明】
1.LinkedList实现了List接口
2. LinkedList的底层使用了双向链表
3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
2. 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 |
五、ArrayList和LinkedList的区别
不同点 | ArrayList | LinkedList |
---|---|---|
存储空间上 | 物理上一定连续 | 逻辑上连续,但物理上不一定连续 |
随机访问 | 支持O(1) | 不支持O(N) |
头插 | 需要挪移元素,效率低O(N) | 只需修改引用的指向,时间复杂度O(1) |
插入 | 空间不够时需要扩容 | 没有容量的概念 |
应用场景 | 元素高效存储+随机访问 | 任意位置插入和删除频繁 |
面试问题:某某 与 某某 的区别
比如:顺序表与链表的区别
回答思路:
从共性出发
- 顺序表和链表都是数据结构,都有着增删查改
- 插入元素时,顺序表由于可能需要移动其他元素,因此时间复杂度达到了O(N),而链表不需要移动元素,只需要修改指向,时间复杂度为O(1)
- 删除元素时与插入元素的情况基本一致
- 查找元素时,顺序表是连续的空间,可以直接利用下标进行查找,因此时间复杂度为O(1),而对于LinkedList双向链表来说,只能从头或者尾节点开始一个节点一个节点进行查找,时间复杂度达到O(N)
- 修改元素的前提是查找元素,然后只是对一个位置的值进行修改,因此情况和查找元素基本一致
- 总结:因此顺序表适合随机访问,链表适合频繁增删的情况
- 二者的的数据结构都是线性表,存储的元素也都放在堆里面,但是存储元素的方式大不相同
- 顺序表是用一段连续的空间进行存储,许多情况需要扩容,因此可能会浪费空间,物理和逻辑上都是连续的,但是由于需要扩容
- 链表是一个一个节点相互引用的数据结构,不一定是连续的空间,但不会出现浪费空间的情况,只在逻辑上连续