JAVA. Implementetion Double Linked list. method remove()

У меня проблема с двумя методами удаления элемента из двусвязного списка, а именно : 1. Метод удаление по объекту. Проблема в том что я не понимаю почему выполняется NullPointerException и возможно, вы сможете предложить другой рабочий вариант реализации данного метода. 2.Метод удаления по индексу. В этом методе получается удалить все индексы, кроме 1-ого...Также пока не пойму почему....

import lombok.AllArgsConstructor;
import lombok.Data;

public class MyLinkedList<E> implements LinkedListMethods<E> {
    public MyLinkedList() {
    }

    @Data
    @AllArgsConstructor
    private static class Node<E> {
        private E e; //element which we wanna add
        private Node<E> next; // reference to next element
        private Node<E> previous; // reference to previous element

        public Node(E e) {
            this.e = e;
        }
    }

    private Node<E> head;
    private Node<E> tail;
    private int size;

    private boolean chekByIndex(int indexOfLinkedList) {
        if (indexOfLinkedList < 0 || indexOfLinkedList > size) {
            return false;
        }
        return true;
    }

    private Node<E> getNodeByIndex(int indexOfLinkedList) {
        Node<E> current = head;
        for (int i = 0; i < indexOfLinkedList; i++) {
            current = current.next;
        }
        return current;
    }

>// in this one i get npe
    @Override
    public void remove(E e) {
        Node<E> current = head;
        while (current != null) {
            if (e.equals(current.e)) {
                current.previous.next = current.next;
                current.next.previous = current.previous;
            }
            size--;
        }
    }

>//here i can't remove the 1st element (index = 0)
    @Override
    public void remove(int indexOfLinkedList) {
        Node<E> current = head;
        if (chekByIndex(indexOfLinkedList)) {
            if (current.previous == null) { // remove 1st element
                current = current.next;
            current.previous = null;
            } else if (current.next == null) { // remove last element
                current = current.previous;
                current.next = null;
            } else {
                Node<E> previousNode = getNodeByIndex(indexOfLinkedList -1);
                Node<E> currentNode = previousNode.next;
                previousNode.next = currentNode.next;
                current.next.previous = previousNode;
            }
            size--;
        }
    }

Ответы (0 шт):