LinkedList与链表
文章目录
1.ArrayList的缺陷
当你底层实现顺序表时,很清楚的知道当你实现中间插入和删除操作时就得挪动数据,这样的时间复杂度为O(n)。
2.链表
2.1链表的概念和结构
链表也是一种线性表,它是一种在物理上不连续,在逻辑上连续的结构。
2.2链表的实现
// 1、无头单向非循环链表实现
public class SingleLinkedList {
//头插法
public void addFirst(int data){}
//尾插法
public void addLast(int data){}
//任意位置插入,第一个数据节点为0号下标
public boolean addIndex(int index,int data){}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){}
//删除第一次出现关键字为key的节点
public void remove(int key){}
//删除所有值为key的节点
public void removeAllKey(int key){}
//得到单链表的长度
public int size(){}
public void display(){}
public void clear(){}
}
3.链表的相关OJ题
- [移除链表元素](203. 移除链表元素 - 力扣(LeetCode))
- [反转链表](206. 反转链表 - 力扣(LeetCode))
- [链表的中间节点](876. 链表的中间结点 - 力扣(LeetCode))
- [链表中倒数第k个节点](链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com))
- [合并两个有序链表](21. 合并两个有序链表 - 力扣(LeetCode))
- [链表分割](面试题 02.04. 分割链表 - 力扣(LeetCode))
- [回文链表](234. 回文链表 - 力扣(LeetCode))
- [相交链表](160. 相交链表 - 力扣(LeetCode))
- [环形链表](141. 环形链表 - 力扣(LeetCode))
请嘎嘎乱刷
4.LinkedList的模拟实现
LinkedList<Integer> list = new LinkedList<>();
LinkedList底层是一个双向链表,所以我们也要自己实现一个双向链表,如果你真的会了单链表的话,这个对于你来说就是手到擒来。
双向链表的结构就是多了一个prev域
public class LinkedList {
static class ListNode{
private int val;
private ListNode next;
private ListNode prev;
public ListNode(int val){
this.val = val;
}
}
public ListNode head;//头节点
public ListNode last;//尾节点
//头插
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;
}
}
//任意位置插入,第一个数据节点为0号下标
public void addIndex(int index, int data){
if(index < 0 || index > size()){
throw new RuntimeException("index位置不合法");
}
ListNode node = new ListNode(data);
ListNode cur = head;
while(index > 0){
index--;
cur = cur.next;
}
if(cur == head){
addFirst(data);
return;
}
node.next = cur;
cur.prev.next = node;
node.prev = cur.prev;
cur.prev = node;
}
//查找是否包含关键字key是否在单链表当中
public boolean contains(int key){
ListNode cur = head;
while(cur != null){
if(cur.val == key){
return true;
}
cur = cur.next;
}
return false;
}
//删除第一次出现关键字key的节点
public void remove(int key){
if(head.next == null){
head = null;
return;
}
if(head.val == key){
head = head.next;
head.prev = null;
return;
}
if(last.val == key){
last = last.prev;
last.next = null;
return;
}
ListNode cur = head;
while(cur != null){
if(cur.val == key){
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
return;
}
cur = cur.next;
}
}
//删除所有值为key的节点
public void removeAllKey(int key){
if(head.next == null){
head = null;
return;
}
if(head.val == key){
head = head.next;
head.prev = null;
}
if(last.val == key){
last = last.prev;
last.next = null;
}
ListNode cur = head;
while(cur != null){
if(cur.val == key){
cur.prev.next = cur.next;
cur.next.prev = cur.prev;
}
cur = cur.next;
}
}
//求链表长度
public int size(){
int count = 0;
ListNode cur = head;
while(cur != null){
count++;
cur = cur.next;
}
return count;
}
public void display(){
ListNode cur = head;
while(cur != null){
System.out.print(cur.val + " ");
cur = cur.next;
}
System.out.println();
}
public void clear(){
head = null;
last = null;
}
}
5.LinkedList的使用
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.addFirst(1);
linkedList.addLast(2);
linkedList.addLast(3);
linkedList.remove();
boolean flg = linkedList.contains(3);
System.out.println(flg);
int val = linkedList.size();
System.out.println(val);
int tmp = linkedList.peek();
System.out.println(tmp);
}
LinkedList中还有很多方法:
6.ArrayList和LinkedList的区别
ArrayList | LinkedList | |
---|---|---|
存储上 | 在物理上连续,逻辑上也连续 | 物理上不连续,逻辑上连续 |
插入删除操作 | 时间复杂度为O(N)(尾插尾删是O(1) | 时间复杂度为O(1) |
查找操作 | O(1) | O(N) |
应用场景 | 适用于查找操作较多的情景 | 适用于插入和删除操作较多的场景 |
这些数据结构创建出来就是用来应对不同场景的,单纯对某个结构来说并没有好坏之分,就看你怎么使用它了