Java数据结构之LinkedList与链表

一、ArrList的缺陷

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

二、链表

(一)链表的概念及结构

1.定义

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

  1. 链式结构在逻辑上是连续的,但是在物理上不一定连续
  2. 现实中的节点一般都是从堆上申请出来的
  3. 从堆上申请的空间,是按照一定的策略分配的,两次申请的空间可能连续,也可能不连续

实际中链表的结构非常多,主要是由于下面几种情况的考虑:

  1. 单向或者双向
  2. 带头结点或者不带头节点
  3. 循环或者非循环

我们主要学习无头单向非循环链表无头双向链表

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的区别

不同点ArrayListLinkedList
存储空间上物理上一定连续逻辑上连续,但物理上不一定连续
随机访问支持O(1)不支持O(N)
头插需要挪移元素,效率低O(N)只需修改引用的指向,时间复杂度O(1)
插入空间不够时需要扩容没有容量的概念
应用场景元素高效存储+随机访问任意位置插入和删除频繁

面试问题:某某 与 某某 的区别
比如:顺序表与链表的区别
回答思路:
从共性出发

  1. 顺序表和链表都是数据结构,都有着增删查改
    • 插入元素时,顺序表由于可能需要移动其他元素,因此时间复杂度达到了O(N),而链表不需要移动元素,只需要修改指向,时间复杂度为O(1)
    • 删除元素时与插入元素的情况基本一致
    • 查找元素时,顺序表是连续的空间,可以直接利用下标进行查找,因此时间复杂度为O(1),而对于LinkedList双向链表来说,只能从头或者尾节点开始一个节点一个节点进行查找,时间复杂度达到O(N)
    • 修改元素的前提是查找元素,然后只是对一个位置的值进行修改,因此情况和查找元素基本一致
    • 总结:因此顺序表适合随机访问,链表适合频繁增删的情况
  2. 二者的的数据结构都是线性表,存储的元素也都放在堆里面,但是存储元素的方式大不相同
    • 顺序表是用一段连续的空间进行存储,许多情况需要扩容,因此可能会浪费空间,物理和逻辑上都是连续的,但是由于需要扩容
    • 链表是一个一个节点相互引用的数据结构,不一定是连续的空间,但不会出现浪费空间的情况,只在逻辑上连续
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

求索1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值