class LinkedList {
constructor() {
this.head = null;
}
add(value) {
if (!this.head) {
this.head = new Node(value);
return;
}
let node = this.head;
while (node.next) {
node = node.next;
}
node.next = new Node(value);
}
delete(target) {
if (this.head.value === target) {
this.head = this.head.next;
return;
}
let node = this.head;
let prev = null;
while (node) {
if (node.value === target && prev) {
prev.next = node.next;
}
prev = node;
node = node.next;
}
}
print() {
let node = this.head;
let linkList = "";
while (node) {
linkList += `${node.value} -> `;
node = node.next;
}
return linkList;
}
has(target) {
let node = this.head;
while (node) {
if (node.value === target) return true;
node = node.next;
}
return false;
}
reverse() {
let previous = null;
let node = this.head;
while (node) {
const temp = node.next;
node.next = previous;
previous = node;
node = temp;
}
this.head = previous;
}
}