Реализация классов LinkedList и Node

class LinkedList {
    constructor() {
        this.root = null
    }}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

Добавить узел

class LinkedList {
    constructor() {
        this.root = null
    }

    add(val) {
        let node = new Node(val)
        if (!this.root) {
            this.root = node;
            return;
        }

        let temp = this.root;

        while(temp.next) {
            temp = temp.next;
        }

        temp.next = node;
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

Добавить узел по заданному индексу

class LinkedList {
    constructor() {
        this.root = null
    }

    addAtIndex(val, index) {
        let newNode = new Node(val);
        if (!this.root && index != 0) return;
        if (index === 0) {
            let temp = this.root;
            this.root = newNode;
            newNode.next = temp;
            return;
        }
        let tempIndex = 0;
        let tempNode = this.root;

        while(tempNode) {
            tempIndex++;
            if (tempIndex === index) break;
            if (!tempNode.next) break;
            tempNode = tempNode.next;
        }

        let nextNode = tempNode.next;
        tempNode.next = newNode;
        newNode.next = nextNode;
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.addAtIndex(1.5, 1);

Добавить узел в начало связанного списка в качестве первого элемента

class LinkedList {
    constructor() {
        this.root = null
    }

    addToFront(val) {
        let newNode = new Node(val);

        if (!this.root) {
            this.root = newNode;
            return;
        }

        let tempRoot = this.root;
        this.root = newNode;
        newNode.next = tempRoot;
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.addToFront(0);

Удалить узел

class LinkedList {
    constructor() {
        this.root = null
    }

    remove(val) {
        if (!this.root) return;
        if (this.root.value == val) {
            this.root = this.root.next;
            return;
        }
        let prevNode = this.root;
        let currentNode = this.root;

        while (currentNode.next) {
            if (currentNode.value == val) break;
            prevNode = currentNode;
            currentNode = currentNode.next;
        }

        let tempNext = currentNode.next;
        currentNode.next = null;
        prevNode.next = tempNext;
        
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.remove(2);

Удалить первый узел

class LinkedList {
    constructor() {
        this.root = null
    }

    removeFirst() {
        if (!this.root) return;
        this.root = this.root.next;
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.removeFirst();

Удалить последний узел

class LinkedList {
    constructor() {
        this.root = null
    }

    removeLast() {
        if (!this.root) return;
        if (!this.root.next) {
            this.root = null;
            return;
        }

        let tempNode = this.root;

        while(tempNode.next.next) {
            tempNode = tempNode.next;
        }

        tempNode.next = null;
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.removeLast();

Обратно связанный список

class LinkedList {
    constructor() {
        this.root = null
    }

    reverse() {
        if (!this.root || !this.root.next) return;

        let currNode = this.root;
        let prevNode = null;
        let nextNode = currNode.next;

        while(currNode) {            currNode.next = prevNode;
            prevNode = currNode;
            currNode = nextNode;
            if (currNode)
            nextNode = currNode.next;
        }
        this.root = prevNode
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.reverse();

Распечатать все узлы

class LinkedList {
    constructor() {
        this.root = null
    }

    print() {
        let array = [];
        
        let temp = this.root;

        while(temp) {
            array.push(temp.value)
            temp = temp.next;
        }

        console.log(array)
    }
}

class Node {
    constructor(val) {
        this.value = val
        this.next = null
    }
}

let ll = new LinkedList();

ll.add(1);
ll.add(2);
ll.add(3);

ll.print()