Sobes.tech
Junior — Middle+

Removing a node and inserting a new element into a doubly linked list

livecode

Task condition

It is necessary to implement functions for deleting and inserting nodes into a doubly linked list structure.

Requirements:

  • Delete a node based on the given value.
  • Insert a new node immediately after the specified node.

Special attention should be paid to boundary situations:

  • Deletion can occur at the beginning, end, or middle of the list.
  • Insertion should work correctly when the list is empty, or when adding an element at the beginning or end.
  • After each operation, the next and prev links of neighboring nodes must be correctly updated.
class Node {
    private Integer value;
    private Node next;
    private Node prev;
}

class LinkedList {
    private Node head;
    private Node tail;

    public void remove(Integer value) {
        // TODO
    }

    public void add(Integer prev, Integer next, Integer value) {
        // TODO
    }
}