LinkedList与链表

1. ArrayList的缺陷

当在ArrayList任意位置插入或者删除元素时,就需要将后序元素整体往前或者往后搬移,时间复杂度为O(n),效率比较低,因此ArrayList不适合做任意位置插入和删除比较多的场景。因此:java 集合中又引入了LinkedList,即链表结构。

2. 链表

2.1 链表的概念及结构

链表是一种物理存储结构上非连续存储结构,数据元素的逻辑顺序是通过链表中的引用链接次序实现的 。

2.2 链表的实现

1.头插法 addFirst()

java 复制代码
public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        head = node;
    }

2.尾插法 addLast()

java 复制代码
public void addList(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

3.addIndex()

java 复制代码
public void addIndex(int index, int data) {
        int len = size();
        // 不合法
        if (index < 0 || index > len) {
            System.out.println("index不合法");
            return;
        }
        // 空链表
        if (index == 0) {
            addFirst(data);
            return;
        }
        // 尾部插入
        if (index == len) {
            addLast(data);
            return;
        }

        //中间位置插入
        ListNode cur = head;
        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        ListNode node = new ListNode(data);
        node.next = cur.next;
        cur.next = node;
    }

4.contains()

java 复制代码
public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

5.remove()

java 复制代码
public void remove(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
        }

        ListNode cur = findNodeOfKey(key);
        if (cur == null) {
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
    }

    private ListNode findNodeOfKey(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

6.removeAllKey()

java 复制代码
public void removeAllKey(int key) {
        ListNode cur = head.next;
        ListNode prev = head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        if (head.val == key) {
            head = head.next;
        }
    }

7.clear()

java 复制代码
 public void clear() {
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        head = null;
    }

8.size()

java 复制代码
public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

9.display()

java 复制代码
public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }
相关推荐
bai_lan_ya3 分钟前
数据结构之线性表
数据结构
大G哥2 小时前
Rust 之 trait 与泛型的奥秘
java·开发语言·jvm·数据结构·rust
suyukangchen2 小时前
深入理解 Linux 阻塞IO与Socket数据结构
linux·数据结构·python
刚入坑的新人编程2 小时前
数据结构——排序(万字解说)初阶数据结构完
数据结构
星沁城4 小时前
236. 二叉树的最近公共祖先
java·数据结构·leetcode·二叉树
懒懒小徐12 小时前
2023华为od统一考试B卷【二叉树中序遍历】
数据结构·算法·华为od
_Itachi__13 小时前
LeetCode 热题 100 138. 随机链表的复制
算法·leetcode·链表
MeiYu_12313 小时前
【数据结构与算法】图的基本概念与遍历
数据结构·c++·学习
evolution_language16 小时前
LintCode第485题-生成给定大小的数组,第220题-冰雹猜想,第235题-分解质因数
数据结构·算法·新手必刷编程50题
举一个梨子zz16 小时前
Java—— 集合 List
java·开发语言·数据结构·intellij-idea