Java源码阅读-LinkedList

本文深入解析了LinkedList的数据结构实现,包括其内部Node类的设计、基本操作如添加、查找和删除元素的具体实现方式,并分析了这些操作的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    transient int size = 0; // 数组长度,transient关键字,序列化的时候忽略该成员

    /**
     * Pointer to first node.
     * Invariant: (first == null && last == null) ||
     *            (first.prev == null && first.item != null)
     */
    transient Node<E> first; // 指向第一个元素的“指针”

    /**
     * Pointer to last node.
     * Invariant: (first == null && last == null) ||
     *            (last.next == null && last.item != null)
     */
    transient Node<E> last; // 指向最后一个元素的“指针”

    /**
     * Constructs an empty list.
     */
    public LinkedList() {
    }

    private static class Node<E> { // 私有的内部类
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) { // 构造函数,需要传入当前元素和前后节点
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }


添加元素

/**
     * Appends the specified element to the end of this list.
     *
     * <p>This method is equivalent to {@link #addLast}.
     *
     * @param e element to be appended to this list
     * @return {@code true} (as specified by {@link Collection#add})
     */
    public boolean add(E e) { // 添加元素的方法
        linkLast(e); // 直接调用inkLast方法
        return true;
    }

/**
     * Links e as last element.
     */
    void linkLast(E e) { // 直接插入到最后,所以时间复杂度是O(1)
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null); // 新的节点,前一节点是插入前的最后一个节点,后一节点为null
        last = newNode; // 插入后最后一个节点为新的节点
        if (l == null) // 插入的是第一个节点,所以也是第一个节点
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
    }


判断是否包含元素

/**
     * Returns {@code true} if this list contains the specified element.
     * More formally, returns {@code true} if and only if this list contains
     * at least one element {@code e} such that
     * <tt>(o==null ? e==null : o.equals(e))</tt>.
     *
     * @param o element whose presence in this list is to be tested
     * @return {@code true} if this list contains the specified element
     */
    public boolean contains(Object o) { // 判断元素是否存在,调用indexOf函数
        return indexOf(o) != -1;
    }

public int indexOf(Object o) { // 查找元素的位置,需要遍历,所以时间复杂度是O(n)
        int index = 0;
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null)
                    return index;
                index++;
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) {
                if (o.equals(x.item))
                    return index;
                index++;
            }
        }
        return -1;
    }


删除元素

/**
     * Removes the first occurrence of the specified element from this list,
     * if it is present.  If this list does not contain the element, it is
     * unchanged.  More formally, removes the element with the lowest index
     * {@code i} such that
     * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>
     * (if such an element exists).  Returns {@code true} if this list
     * contained the specified element (or equivalently, if this list
     * changed as a result of the call).
     *
     * @param o element to be removed from this list, if present
     * @return {@code true} if this list contained the specified element
     */
    public boolean remove(Object o) {
        if (o == null) {
            for (Node<E> x = first; x != null; x = x.next) {
                if (x.item == null) {
                    unlink(x);
                    return true;
                }
            }
        } else {
            for (Node<E> x = first; x != null; x = x.next) { // 遍历链表找到该元素
                if (o.equals(x.item)) {
                    unlink(x);
                    return true;
                }
            }
        }
        return false;
    }
/**
     * Unlinks non-null node x.
     */
    E unlink(Node<E> x) { // 释放该元素,修改前后元素的“指针”
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next;
        final Node<E> prev = x.prev;

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值