目录
一、ArrayList的缺陷
通过源码知道,ArrayList底层使用数组存储元素。由于其底层是一段连续存储空间,当ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(N),效率较低,因此ArrayList不适合做任意位置频繁的插入和删除比较多的场景。因此:Java集合中又引入了LinkList,即链表结构。
二、链表
1. 链表的概念及结构
(1)什么是链表呢?在这里我们给一张图:
这是小火车,由车头拉着车厢在走,车厢之间是通过一个铁钩相连的。那么链表也是类似的,是链式存储数据的,它把每一个数据放到一个个的节点里面,然后把节点和节点之间通过某种关系串起来,这样的结构就叫做链表。
也就是说,将数据元素通过节点进行存储,然后使用一个叫做next的引用/指针将这些节点串联起来。这样的结构我们就把它叫做链表。
(2)认识链表结构
链表的结构总共有八种,从方向来说有单向和双向,从循环和非循环来说有循环和非循环的链表,然后还可以分带头节点和不带头节点的链表。那么我们可以组合一下:
单向(双向)循环带头结点、单向(双向)循环不带头结点、单向(双向)非循环带头结点、单向(双向)非循环不带头结点。
我们主要学习的是单向(双向)非循环不带头结点,我们以后接触到最多的就是单向非循环不带头结点,然后也要再了解双向非循环不带头结点。
我们列出一组数据:12 23 34 45 56 67 78 89,我们需要把这组数据存到单链表里面去。
首先,链表是由结点组成的,我们的每一个数据都需要放到结点里面,那么这个结点至少会由两个部分组成(两个域):一部分是数字val,一部分是地址,也就是一个节点对象:
我们先把数据画出来:
我们让每个节点的next存放下一个节点的地址,于是每个节点就有了“方向”,如图所示,这样就构成了一个单向非循环不带头结点的一个链表。
从中可看出:对于链表本身来说,它在物理内存上不一定连续,从逻辑上来说是连续的,而对于顺序表来说,顺序表在物理和逻辑上都是连续的。
注:我们后续对链表的增删查改都只讨论上图这种单向非循环不带头结点的结构,即单链表。
带头结点:规定第一个节点的数据是无意义的,而且这个地方永远标志链表的头。
循环的:
2. 链表的实现
(1)我们暂时先用最简单的方法来构造一个单链表:
public class MySingleList {
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//不初始化了 默认是null
public void createList() {
ListNode listNode1 = new ListNode(12);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(34);
ListNode listNode4 = new ListNode(45);
ListNode listNode5 = new ListNode(56);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
this.head = listNode1;
}
}
可以在测试类中打印出我们自己创建的链表的结果:
public class Test {
public static void main(String[] args) {
MySingleList mySingleList = new MySingleList();
mySingleList.createList();
mySingleList.display();//打印链表
}
}
运行结果:
(2)链表具体操作的实现
public class MySingleList {
/**
* 节点内部类
*/
static class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
public ListNode head;//不初始化,默认null
//自己构造一个单链表
public void createList() {
ListNode listNode1 = new ListNode(12);
ListNode listNode2 = new ListNode(23);
ListNode listNode3 = new ListNode(34);
ListNode listNode4 = new ListNode(45);
ListNode listNode5 = new ListNode(56);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
this.head = listNode1;
}
// 打印链表中的数据
public void display() {
ListNode cur = this.head; // 要点1:使头结点指针不动,定义cur节点来进行遍历
// 不可以是 cur.next == null, 否则无法打印cur.val
while (cur != null) { // 要点2:读取cur值的循环条件
System.out.print(cur.val + " ");
cur = cur.next; // 要点3:如何让cur往下走
}
System.out.println();
}
// 从指定节点newHead开始打印链表
public void display(ListNode newHead) {
ListNode cur = newHead;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
// 得到单链表长度: 只需知道有多少个节点
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
// 查找是否包含关键字key是否在单链表中
public boolean contains(int key) {
ListNode cur = this.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;
}
// 链表插入比起顺序表的好处: 1. 插入数据时不用挪动元素; 2. 只需要修改指向即可
// 尾插法
public void addLast(int data) {
ListNode node = new ListNode(data);
ListNode cur = this.head; // 定义一个cur, 一直走到它指向节点的next为空的时候, 就是尾巴
// 注意头结点为空的情况
if (cur == null) {
this.head = node;
} else {
while (cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
}
// 任意位置插入,第一个数据节点为0号下标(这里的下标为 数学逻辑层面定义的下标, 而非代码层面)
public void addIndex(int index, int data) {
// 考虑index位置不同导致的多种情况
// 1. 位置小于零或者大于链表长度,无法插入
if (index < 0 || index > size()) {
System.out.println("index位置不合法");
throw new IndexWrongFulException("index位置不合法");
}
// 2. 在头位置插入, 即头插
if (index == 0) {
addFirst(data);
return;
}
// 3. 在尾巴插入, 即尾插
if (index == size()) {
addLast(data);
return;
}
// 4. 中间任意位置插, 核心逻辑
ListNode cur = findIndexSunOne(index); // 找到cur, 先让 cur 走 index-1 步
ListNode node = new ListNode(data);
// 修改指向
node.next = cur.next;
cur.next = node;
}
// 找 index-1 位置的节点
private ListNode findIndexSunOne(int index) {
ListNode cur = this.head;
while (index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
// 删除第一次出现关键字为key的节点
public void remove(int key) {
// 删的正好是为空的头结点
if (this.head == null) {
return;
}
// 删的头结点的值正好为key
if (this.head.val == key) {
this.head = this.head.next;
return;
}
// 找key
ListNode cur = findPrevOfKey(key);
if (cur == null) {
System.out.println("没有你要删除的数字");
return;
}
// 删key
ListNode del = cur.next;
cur.next = del.next;
}
// 找key的前驱节点
private ListNode findPrevOfKey(int key) {
ListNode cur = this.head;
while (cur.next != null) {
if (cur.next.val == key) {
return cur;
}
cur = cur.next;
}
return null;
}
// 删除所有值为key的节点. 要求时间复杂度为O(N), 即只遍历链表一遍.
public void removeAllKey(int key) {
// 要先判断链表是否为空, 否则后面 head.next 可能报空指针异常
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;
}
}
// 前面忽略了头节点是否为key, 如果是则删除.
if (this.head.val == key) {
this.head = this.head.next;
}
}
// 清空所有节点
public void clear() {
this.head = null;
}
}
三、链表面试题
(1)删除链表中等于给定值 val 的所有节点。OJ链接
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
示例:
总体代码逻辑与上文中链表操作代码几乎相同,注意一下在OJ中不需要加 this ,因为this指向的是当前对象Solution的引用,而在removeElements中的OJ的代码已经给出了 ListNode head 了,所以在 head 与 cur 等代码不需要加 this 。
class Solution {
public ListNode removeElements(ListNode head, int val) {
//头节点为空
if (head == null) {
return null;
}
//代码要求O(N),所以定义两个节点同时走,也便于删除时寻找前后节点
ListNode cur = head.next;
ListNode prev = head;
while (cur != null) {
//值满足条件时删除节点,否则控制两个节点指针往下遍历
if (cur.val == val) {
prev.next = cur.next;
cur = cur.next;
} else {
prev = cur;
cur = cur.next;
}
}
//头结点的值刚好满足条件时
if (head.val == val) {
head = head.next;
}
return head;
}
}
(2)反转链表。OJ链接
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
示例:
该题是面试必考题.
注意, 如下思路是错误的: 遍历链表,把值存储在数组当中,然后逆置数组.
这个题目的目的是改变 这个链表本身的每个节点的指向.
class Solution {
public ListNode reverseList(ListNode head) {
//1. 没有节点
if (head == null){
return null;
}
//2. 只有一个节点
if (head.next == null){
return head;
}
//3. 两个以上节点,记录下cur.next然后依次进行头插
ListNode cur = head.next;
head.next = null;
while (cur != null){
ListNode curNext = cur.next;
cur.next = head;
head = cur;
cur = curNext;
}
return head;
}
}
(3)给定一个头结点为 head
的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。OJ链接
class Solution {
public ListNode middleNode(ListNode head) {
// 求链表的中间节点
// 使用快慢指针,速度与路程相关的数学原理,注意循环条件
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
(4)链表中倒数第k个节点。OJ链接
输入一个链表,输出该链表中倒数第k个结点。
public class Solution {
public ListNode FindKthToTail(ListNode head, int k) {
// 1. k位置不合法
if (k <= 0 || head == null) {
return null;
}
// 2. 定义快慢指针确定寻找位置。先让fast走k-1步,然后fast与slow一起走
ListNode fast = head;
ListNode slow = head;
while (k - 1 != 0) {
fast = fast.next;
if (fast == null) {
return null;
}
k--;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
(5)合并有序链表。OJ链接
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
示例:
class Solution {
// 合并两个有序链表
// 利用虚拟节点/傀儡节点确定合并之后链表的头结点,
// 然后用cur确定下一个节点的位置
// 如何确定?比较两个链表对应head遍历到的节点位置的大小
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
ListNode newHead = new ListNode(-1);
ListNode cur = newHead;
//两链表不为空时才能合并
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
cur.next = head1;
head1 = head1.next;
} else {
cur.next = head2;
head2 = head2.next;
}
cur = cur.next;
}
//head2遍历完成,为空时剩下的合并链表就是head1指向的剩余链表
if (head1 != null) {
cur.next = head1;
}
//head1遍历完成,为空时剩下的合并链表就是head2指向的剩余链表
if (head2 != null) {
cur.next = head2;
}
// 返回虚拟节点的下一个节点
return newHead.next;
}
}
(6)链表分割。(字节面试题)OJ链接
现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。
题意:结果链表由两部分组成一个新的链表,一部分是小于 x 的,另一部分是大于 x 的。
所以将这两部分构成的链表进行拼接即可。
import java.util.*;
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Partition {
public ListNode partition(ListNode pHead, int x) {
ListNode bs = null;
ListNode be = null;
ListNode as = null;
ListNode ae = null;
// 定义cur进行链表的遍历, 当cur为 null 的时候, 链表遍历完成
ListNode cur = pHead;
while (cur != null) {
if (cur.val < x) {
// 第一次进行插入节点
if (bs == null) {
bs = cur;
be = cur;
} else {
// 不是第一次进行插入节点
be.next = cur;
be = be.next;
}
} else {
if (as == null) {
as = cur;
ae = cur;
} else {
ae.next = cur;
ae = ae.next;
}
}
cur = cur.next; // cur往后走
}
// 如果: 第一个段 没有数据
if (bs == null) {
return as;
}
be.next = as; // 连接两个段, 注意be为空情况, 所以在前面加上
// 手动置空一次!(尾结点的 next 值可能为空)
if (as != null) {
ae.next = null;
}
return bs;
}
}
(7)编写链表的回文结构。OJ链接
对于一个链表,请设计一个时间复杂度为O(n),额外空间复杂度为O(1)的算法,判断其是否为回文结构。
给定一个链表的头指针A,请返回一个bool值,代表其是否为回文结构。保证链表长度小于等于900。
注意:
[申请一个数组,把链表遍历一遍,把val值存到数组中,判断数组中的数据是否回文]
该思路的空间复杂度达到O(N),不符题意
public class PalindromeList {
public boolean chkPalindrome(ListNode head) {
//1. 只有一个节点,就是回文结构了
if (head == null) {
return false;
}
if (head.next == null) {
return true;
}
//2. 找到链表的中间节点 - 快慢指针
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
//3. 反转中间节点后的链表
ListNode cur = slow.next;
while (cur != null) {
ListNode curNext = cur.next;
cur.next = slow;
slow = cur;
cur = curNext;
}
//判断是否回文
while (head != slow) {
if (head.val != slow.val) {
return false;
}
if (head.next == slow) {
return true;
}
head = head.next;
slow = slow.next;
}
return true;
}
}
(8)相交链表。OJ链接
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。
图示两个链表在节点 c1 开始相交:
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
// 1. 求链表的长度(为了让逻辑更加清楚, 我们多定义几个变量)
ListNode pl = headA; // pl 永远指向 最长的链表
ListNode ps = headB; // ps 永远指向 最短的链表
// 求第一个链表长度
int lenA = 0;
while(pl != null){
lenA++;
pl = pl.next;
}
// 求第二个链表长度
int lenB = 0;
while(ps != null){
lenB++;
ps = ps.next;
}
pl = headA; // 求完长度后pl为空了, 需要pl指回headA
ps = headB; // 求完长度后ps为空了, 需要ps指回headB
// pl,ps指回之后才能说, 它们两个分别指向最长, 最短的链表
int len = lenA - lenB; // 求差值, 并判断谁长
if(len < 0){
pl = headB;
ps = headA;
len = lenB - lenA; // len为负数, 修正为整数, 以便后续走len步
}
// 2. 让最长的链表走len步,使pl和ps处于距离相交点 一样远的位置
while(len != 0){
pl = pl.next;
len--;
}
// 3. 控制指针其相遇
while(pl != ps){
pl = pl.next;
ps = ps.next;
}
// 易漏逻辑,走到此处pl和ps相等了,但是也可能会是两个链表没有交点的情况
if(pl == null){
return null;
}
return pl;
}
}
(9)环形链表1。OJ链接
给你一个链表的头节点 head ,判断链表中是否有环。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。注意:pos 不作为参数进行传递 。仅仅是为了标识链表的实际情况。
如果链表中存在环 ,则返回 true 。 否则,返回 false 。
解题思路:
快慢指针,即慢指针一次走一步,快指针一次走两步,两个指针从链表起始位置开始运行,
如果链表带环则一定会在环中相遇,否则快指针率先走到链表的末尾.
为什么快指针每次走两步,慢指针走一步可以?
假设链表带环,两个指针最后都会进入环,快指针先进环,慢指针后进环。当慢指针刚进环时,可能就和快指针相遇了,最差情况下两个指针之间的距离刚好就是环的长度。此时,两个指针每移动一次,之间的距离就缩小一步,不会出现每次刚好是套圈的情况,因此:在满指针走到一圈之前,快指针肯定是可以追上慢指针的,即相遇。
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while (fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if (fast == slow){
return true;
}
}
return false;
}
}
(10)环形链表2。OJ链接
给定一个链表的头节点 head
,返回链表开始入环的第一个节点。 如果链表无环,则返回 null
。
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
if(fast == slow){
break;
}
}
if(fast == null || fast.next == null){
return null;
}
slow = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
}
四、LinkList的模拟实现
LinkList底层是一个双向链表,每一个包含了三个域:val域、next域、prev域,如图例所示链表为一个双向链表。
1. 模拟实现双向链表
public class MyLinkList {
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 display() {
ListNode cur = this.head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//得到链表的长度
public int size() {
int count = 0;
ListNode cur = this.head;
while (cur != null) {
count++;
cur = cur.next;
}
return count;
}
//查找是否包含关键字key是否在单链表中
public boolean contains(int key) {
ListNode cur = this.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 (this.head == null) {
this.head = node;
this.tail = node;
} else {
node.next = this.head;
this.head.prev = node;
head = node;
}
}
//尾插
public void addLast(int data) {
ListNode node = new ListNode(data);
if (this.head == null) {
this.head = node;
this.tail = node;
} else {
tail.next = node;
node.prev = tail;
tail = node;
}
}
//任意位置插入,第一个数据节点为 0 的下标
public void addIndex(int index, int data) {
//1. 判断 Index 位置的合法性
if (index < 0 || index > size()) {
System.out.println("index位置不合法!");
throw new IndexWrongfulException("index位置不合法!");
}
//2. 判断特殊位置, 头插 和 尾插
if (index == 0) {
addFirst(data);
return;
}
if (index == size()) {
addLast(data);
return;
}
//3. 找到 Index 位置的地址
ListNode cur = findIndexListNode(index);
//4. 修改 4 个指向
ListNode node = new ListNode(data);
node.next = cur;
node.prev = cur.prev;
cur.prev.next = node;
cur.prev = node;
}
public ListNode findIndexListNode(int index) {
ListNode cur = this.head;
while (index != 0) {
index--;
cur = cur.next;
}
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 {
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 (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 void clear() {
ListNode cur = head;
while (cur != null) {
ListNode curNext = cur.next;
cur.prev = null;
cur.next = null;
cur = curNext; // 记录下一个cur
}
head = null;
tail = null;
}
}
2. LinkList的使用
LinkList 源码中有许多Java自身已经写好的代码接口,我们可以将数据进行整合成一种数据结构,并且将这种数据结构再置入LinkList(暂不探讨)。比如说将许多单链表的头放入LinkList中,就可以构造出一种新的数据结构。以下附上 LinkList 常用方法:
方法
|
解释
|
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<E>
subList
(int fromIndex, int toIndex)
|
截取部分
list
|
我们接着来了解一下 LinkList 的遍历。
首先构造以下list:
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
}
于是有以下三种方式遍历:
1. for-each遍历
for (int e : list) {
System.out.print(e + " ");
}
System.out.println();
2. 迭代器遍历:正向遍历
ListIterator<Integer> listIterator = list.listIterator();
while (listIterator.hasNext()){
System.out.print(listIterator.next() + " ");
}
System.out.println();
listInterator中未指定参数则默认从头开始,指定size参数可以从后往前遍历。
3. 反向遍历
ListIterator<Integer> listIterator = list.listIterator(list.size());
while (listIterator.hasPrevious()) {
System.out.print(listIterator.previous() + " ");
}
System.out.println();
五、ArrayList 和 LinkList 的区别
在面试中可能会问到如下的问题:
1. 数组和链表的区别是什么?
2. 顺序表和,链表的区别是什么?
3. ArrayList 和 LinkList 的区别是什么?
实际上以上的问题都是一个问题,面对这样的问题可以从共性出发回答问题。
那么不管是ArrayList也好LinkList也好,它们都是Java的集合类,背后的数据结构是顺序表和链表,而且这个链表是双向的链表,那么这些数据结构决定了数据的组织和描述方式,在这里面的第一个,它们都有增删查改(于是可以从增删查改方面述说),比如说插入元素的时候,如果对于顺序表,比如往0位置插入的时候,这里需要先移动元素然后插入元素,会需要把原来的顺序表所有元素都挪开,其时间复杂度为O(n),而对于链表来说只需要O(1)即可,因为只需要修改指向即可。所以插入元素的两者区别就在于移动元素。
注意顺序表的优点是随机访问,也就是给定下标情况下能直接找到元素,效率非常高。而对于链表来说链表比较适合插入和删除比较频繁的情况。
第二个方面是存储方面,顺序表是一个数组,是一块连续的内存,当插入元素时需要扩容的情况下,就有可能出现浪费空间的情况。而链表就非常适合于随用随取。
不同点
|
ArrayList
|
LinkedList
|
---|---|---|
存储空间上
|
物理上一定连续
|
逻辑上连续,但物理上不一定连续
|
随机访问
|
支持
O(1)
|
不支持:
O(N)
|
头插
|
需要搬移元素,效率低
O(N)
|
只需修改引用的指向,时间复杂度为
O(1)
|
插入
|
空间不够时需要扩容
|
没有容量的概念
|
应用场景
|
元素高效存储
+
频繁访问
|
任意位置插入和删除频繁
|