Java LinkedList与链表

目录

LinkedList

什么是LinkedList?

 LinkedList的使用

LinkedList的常用方法

 链表

链表的概念

 1.单向或双向

 2.带头或不带头

 3.循环或非循环

 链表的实现

无头单向非循环链表的实现

带头双向循环链表的实现

ArrayList与LinkedList的区别​


LinkedList

什么是LinkedList?

LinkedList的底层是双向链表结构,由于链表没有将元素存储在连续的空间中,元素存储在单独的节点中,然后通过引用将节点连接起来了,因此在在任意位置插入或者删除元素时,不需要搬移元素,效率比较高。

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

 1. LinkedList实现了List接口

2. LinkedList的底层使用了双向链表

3. LinkedList没有实现RandomAccess接口,因此LinkedList不支持随机访问

4. LinkedList的任意位置插入和删除元素时效率比较高,时间复杂度为O(1)

 LinkedList的使用

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的常用方法

 链表

链表的概念

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

 1.单向或双向

 

 2.带头或不带头

 

 3.循环或非循环

 

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

无头双向链表:在Java的集合框架库中LinkedList底层实现就是无头双向循环链表。

 链表的实现

无头单向非循环链表的实现

import java.util.Stack;

class ListNode {
    public int val;
    public ListNode next;//类型是Node  null

    public ListNode(int val) {
        this.val = val;
    }
}
public class SingleLinkedList {

    public ListNode head;//为什么定义到这里
    public int usedSize;//记录当前链表中节点的个数

    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);
        //int a = 10;
        node1.next = node2;//?
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
        this.usedSize = 5;
    }


    /**
     * 打印链表里面的元素
     */
    public void myToString(){
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 从指定的位置开始打印链表
     * @param newHead
     */
    public void myToString(ListNode newHead){
        ListNode cur = newHead;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while (cur != null) {
            //equals
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //得到单链表的长度-》尝试不用我们定义的usedSize
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public int size2(){
       return this.usedSize;
    }

    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
        }else {
            node.next = head;
            head = node;
        }
        this.usedSize++;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
        }else {
            ListNode cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            //cur.next == null
            cur.next = node;
        }
        this.usedSize++;
    }

    /**
     * 查找Index-1位置的节点的地址
     * @param index
     * @return
     */
    private ListNode searchIndex(int index) {
        int count = 0;
        ListNode cur = this.head;
        while (count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > this.usedSize) {
            throw new RuntimeException("index不合法!");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == this.usedSize) {
            addLast(data);
            return;
        }
        ListNode listNode = new ListNode(data);
        ListNode cur = searchIndex(index);
        listNode.next = cur.next;
        cur.next = listNode;
        this.usedSize++;//这里注意 该加要加  该减要减
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null) {
            return;
        }

        if(this.head.val == key) {
            this.head = this.head.next;
            return;
        }
        ListNode prev = findPrevNode(key);
        if(prev == null) {
            throw new RuntimeException("不存在你要删除的节点!");
        }
        ListNode del = prev.next;
        prev.next = del.next;
        this.usedSize--;
    }

    /**
     * 找到关键字Key的前一个节点
     * @param key
     * @return 不存在 返回null 存在返回节点的地址
     */
    private ListNode findPrevNode(int key) {
        ListNode prev = this.head;
        while (prev.next != null) {
            if(prev.next.val == key) {
                return prev;
            }
            prev = prev.next;//????????
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        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;
                cur = cur.next;
                this.usedSize--;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key) {
            this.head = this.head.next;
            this.usedSize--;
        }
    }

    /**
     * 清空链表
     */
    public void clear1(){
        this.head = null;
        this.usedSize = 0;
    }

    public void clear (){
        ListNode cur = this.head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.usedSize = 0;
    }
    //shift+f6
    public ListNode reverseList() {
        if(this.head == null) {
            return null;
        }
        if(this.head.next == null) {
            return this.head;
        }
        //最起码有2个节点以上
        ListNode cur = this.head.next;
        this.head.next = null;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = this.head;
            this.head = cur;
            cur = curNext;
        }
        return this.head;
    }

    /**
     * 找到链表的中间节点
     * @return
     */
    public ListNode middleNode() {
        ListNode fast = head;
        ListNode slow = head;
       while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 找到单链表 倒数第K个节点
     * 要去 只遍历链表一遍
     * @param k
     * @return
     */
    public ListNode findKthToTail(int k) {
        //k是否是合法的
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        while (k-1 > 0) {
            if(fast.next == null) {
                return null;
            }
            fast = fast.next;
            /*if(fast == null) {
                return null;
            }*/
            k--;
        }
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    /**
     * 以X为基准 进行分割链表
     * @param pHead
     * @param x
     * @return
     */
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        if(pHead == null) {
            return null;
        }

        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = 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;
        }
        if(bs == null) {
            return as;
        }
        be.next = as;
        //检查最后一段是不是有数据,如果有,那么我们需要进行把最后一个节点的next置为空,否则会出现死循环
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }

    /**
     * 判断链表是不是回文链表
     * @return
     */
    public boolean chkPalindrome() {
        if(head == null) {
            return true;
        }
        if(head.next == null) {
            return true;
        }
        //1、找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2、slow已经到中间位置了,开始翻转
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3、slow已经到最后一个节点了,开始往回走
        while(head != slow) {
            if(head.val != slow.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    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;
    }

    /**
     * 判断链表是否有环
     * @param head
     * @return
     */
    public boolean hasCycle2(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 false;
        }
        return true;
    }

    /**
     * 求环的入口点
     * @param head
     * @return
     */
    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;
        }

        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    /**
     * 递归逆序打印单链表
     * @param head
     */
    public void printList(ListNode head) {
        if(head == null) {
            return;
        }
        if(head.next == null) {
            System.out.print(head.val+" ");
            return;
        }
        printList(head.next);
        System.out.print(head.val+" ");
    }

    public void printList2() {
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        while (!stack.empty()) {
            ListNode tmp = stack.pop();
            System.out.print(tmp.val+" ");
        }
        System.out.println();
    }
}

带头双向循环链表的实现

import java.awt.*;

class ListNode {
    public int val;//值
    public ListNode next;//后继信息
    public ListNode prev;//前驱信息

    public ListNode(int val) {
        this.val = val;
    }
}

public class MyLinkedList {

    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不合法!");
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == size()) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);

        ListNode cur = findIndex(index);
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
    }

    private ListNode findIndex(int index) {
        ListNode cur = this.head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return 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;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                //判断是不是头节点
                if(cur == head) {
                    head = head.next;
                    //判断是不是只有1个节点
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    //两种情况:1、是中间节点   2、是尾巴节点
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        //不是尾巴节点 是中间节点
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴节点
                        last = last.prev;
                    }
                }
                return;//删除结束 return走人
            }else {
                cur = cur.next;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                //判断是不是头节点
                if(cur == head) {
                    head = head.next;
                    //判断是不是只有1个节点
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    //两种情况:1、是中间节点   2、是尾巴节点
                    cur.prev.next = cur.next;
                    if(cur.next != null) {
                        //不是尾巴节点 是中间节点
                        cur.next.prev = cur.prev;
                    }else {
                        //尾巴节点
                        last = last.prev;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    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 = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            //cur.val = null;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        last = null;
    }
}

ArrayList与LinkedList的区别

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值