Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
912 B
2
Indexable
Never
public T remove(int index) {
        checkIndexBound(index);
        T element = null;
        if (head == null) {
            return null;
        }
        if (size == 1) {
            element = head.data;
            head = tail = null;
            size--;
            return element;
        } else if (index == 0) {
            element = head.data;
            head = head.next;
            head.prev = null;
            size--;
            return element;
        } else if (index == size - 1) {
            element = tail.data;
            tail = tail.prev;
            tail.next = null;
            size--;
            return element;
        } else {
            Node<T> node = (Node<T>) get(index - 1);
            element = node.next.data;
            node.next = node.next.next;
            node.next.prev = node;
        }
        size--;
        return element;
    }
Leave a Comment