目录
[1. 链表](#1. 链表)
[1.1 链表的概念及结构](#1.1 链表的概念及结构)
[2. 无头单向非循环链表实现](#2. 无头单向非循环链表实现)
[4.1 什么是LinkedList](#4.1 什么是LinkedList)
[4.2 LinkedList的使用](#4.2 LinkedList的使用)
[5. ArrayList和LinkedList的区别](#5. ArrayList和LinkedList的区别)
1. 链表
1.1 链表的概念及结构
链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

实际中链表的结构非常多样,以下情况组合起来就有8种链表结构:
1. 单向或者双向

2. 带头或者不带头

3. 循环或者非循环

虽然有这么多的链表的结构,但是我们重点掌握两种:
**无头单向非循环链表:**结构简单,一般不会单独用来存数据。实际中更多是作为其他数据结构的子结构,如 哈希桶、图的邻接表等等。另外这种结构在笔试面试中出现很多。

**无头双向链表:**在Java的集合框架库中LinkedList底层实现就是无头双向循环链表 。
2. 无头单向非循环链表实现
java
public class MySingleList {
class ListNode {
public int val;
public ListNode next;
public ListNode(int val) {
this.val = val;
}
}
//头节点
public ListNode head;
public void createList() {
ListNode node1 = new ListNode(12);
ListNode node2 = new ListNode(23);
ListNode node3 = new ListNode(34);
ListNode node4 = new ListNode(45);
ListNode node5 = new ListNode(56);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
this.head = node1;
}
//遍历单链表
public void show() {
ListNode cur = head;
while(cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//得到单链表的长度 --》 链表中节点的个数
public int size() {
ListNode cur = head;
int count = 0;
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);
if(head == null) {
head = node;
return;
}
ListNode cur = head;
while(cur.next != null) {
cur = cur.next;
}
cur.next = node;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data) {
if(index < 0 || index > size()) {
throw new IndexOfBounds("插入数据时, index位置不合法, 此时, index: " + index);
}
if(0 == index) {
addFirst(data);
return;
}
if (size() == index) {
addLast(data);
return;
}
// ListNode node = new ListNode(data);
// ListNode preCur = head;
// ListNode cur = head.next;
// while(index-1 != 0) {
// cur = cur.next;
// preCur = preCur.next;
// index--;
// }
// node.next = cur;
// preCur.next = node;
//找到插入位置的前一个节点
ListNode cur = findIndex(index);
//插入
ListNode node = new ListNode(data);
node.next = cur.next;
cur.next = node;
}
// 保存插入节点的前驱节点
public ListNode findIndex(int index) {
ListNode cur = head;
while(index - 1 != 0) {
cur = cur.next;
index--;
}
return cur;
}
//删除第一次出现关键字为key的节点
public void remove(int key) {
if(head == null) {
return;
}
if(head.val == key) {
head = head.next;
return;
}
ListNode prev = searchPrev(key);
if(prev == null) {
System.out.println("没有你要删除的数据");
return;
}
ListNode del = prev.next;
prev.next = del.next;
}
// 保存被删除的前驱节点
public ListNode searchPrev(int key) {
ListNode prev = head;
while(prev.next != null) {
if(prev.next.val == key) {
return prev;
}
prev = prev.next;
}
return null;
}
//删除所有值为key的节点
public void removeAllKey(int key) {
if(head == null) {
return;
}
ListNode prev = head;
ListNode cur = head.next;
while(cur != null) {
if(cur.val == key) {
prev.next = cur.next;
cur = cur.next;
}else {
cur = cur.next;
prev = prev.next;
}
}
if(head.val == key) {
head = head.next;
}
}
// 清空链表
public void clear() {
this.head = null;
}
}
3.LinkedList的模拟实现
java
public 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 last;
public int size() {
int len = 0;
ListNode cur = head;
while (cur != null) {
len++;
cur = cur.next;
}
return len;
}
// 打印
public void display() {
ListNode cur = head;
while (cur != null) {
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
//查找是否包含关键字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;
return;
}
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;
return;
}
last.next = node;
node.prev = last;
last = node;
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index,int data) {
int size = size();
if(index < 0 || index > size) {
throw new IndexOfBounds("双向链表index不合法!");
}
if(index == 0) {
addFirst(data);
return;
}
if(index == size()) {
addLast(data);
return;
}
ListNode cur = head;
while(index != 0) {
cur = cur.next;
index--;
}
ListNode node = new ListNode(data);
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
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;
}
return;
}
// 删除的如果是尾节点
if(cur == last) {
last = last.prev;
last.next = null;
return;
}
// 删除的如果是中间节点
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
return;
}else {
cur = cur.next;
}
}
System.out.println("没有你要删除的节点!");
}
//删除所有值为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 == last) {
// 删除的如果是尾节点
last = last.prev;
last.next = null;
}else {
// 删除的如果是中间节点
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
}
cur = cur.next;
}
}
// 清空
public void clear() {
ListNode cur = head;
while (cur != null) {
ListNode curNext = cur.next;
cur.prev = null;
cur.next = null;
}
head = null;
last = null;
}
}
4.LinkedList的使用
4.1 什么是LinkedList
LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节 点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。

在集合框架中,LinkedList也实现了List接口,具体如下:

-
LinkedList实现了List接口
-
LinkedList的底层使用了双向链表
-
LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问
-
LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)
-
LinkedList比较适合任意位置插入的场景
4.2 LinkedList的使用
- LinkedList的构造

java
public static void main(String[] args) {
// 构造一个空的LinkedList
List<Integer> list1 = new LinkedList<>();
List<String> list2 = new java.util.ArrayList<>();
list2.add("JavaSE");
list2.add("JavaWeb");
list2.add("JavaEE");
// 使用ArrayList构造LinkedList
List<String> list3 = new LinkedList<>(list2);
}
- LinkedList的其他常用方法介绍

- LinkedList的遍历
java
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());
// foreach遍历
for (int e:list) {
System.out.print(e + " ");
}
System.out.println();
// 使用迭代器遍历---正向遍历
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
System.out.print(it.next()+ " ");
}
System.out.println();
// 使用反向迭代器---反向遍历
ListIterator<Integer> rit = list.listIterator(list.size());
while (rit.hasPrevious()){
System.out.print(rit.previous() +" ");
}
System.out.println();
}
5. ArrayList和LinkedList的区别

ArrayList适合用于"读多写少"的场景,因为它的随机访问性能较好;而LinkedList则更适合于频繁的插入和删除操作,尤其是在列表的两端,因为它能够在这些情况下提供更好的性能。然而,LinkedList的内存开销通常比ArrayList要大,因为每个节点都需要维护额外的引用信息。