class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor(value) {
this.head = {
value: value,
next: null
};
this.tail = this.head;
this.length = 1;
}
append(value) {
const newNode = new Node(value);
this.tail.next = newNode;
this.tail = newNode;
this.length++;
return this;
}
prepend(value) {
const newNode = new Node(value);
newNode.next = this.head;
this.head = newNode;
this.length++;
return this;
}
traverseToIndex(index) {
let counter = 0;
let currentNode = this.head;
while (counter !== index) {
currentNode = currentNode.next;
counter++;
}
return currentNode;
}
insert(index, value) {
if (index === 0) {
return this.prepend(value);
}
if (index >= this.length) {
return this.append(value);
}
// let curr_index = 0;
// let node = this.head;
// while (curr_index < (index - 1)) {
// node = node.next;
// curr_index++;
// }
let node = this.traverseToIndex(index - 1);
const newNode = new Node(value);
const temp = node.next;
node.next = newNode;
newNode.next = temp;
this.length++;
return this;
}
remove (index) {
let node = null;
if (index === 0) {
node = this.head;
let temp = node.next;
// node.next = null;
node = temp;
this.length--;
this.head = node;
return this;
}
node = this.traverseToIndex(index-1);
let nodeToBeRemoved = node.next;
let successor = nodeToBeRemoved.next;
// nodeToBeRemoved.next = null;
node.next = successor;
this.length--;
return this;
}
// print the elements in the linked list
printList() {
const values = [];
let node = this.head;
while (node !== null) {
values.push(node.value);
node = node.next;
}
console.log(values.join(" => "));
}
//reverse the elements in the linked list
reverse() {
let node = this.head;
const newLinkedList = new LinkedList(node.value);
while (node.next !== null) {
newLinkedList.prepend(node.next.value);
node = node.next;
}
return newLinkedList;
}
reverse2() {
if (this.head.next === null) {
return this;
}
let left = this.head;
this.tail = this.head;
let middle = left.next;
while (middle) {
let right = middle.next;
middle.next = left;
left = middle;
middle = right;
}
this.head.next = null;
this.head = left;
return this;
}
}
// -----------------------
const myLinkedList = new LinkedList(10);
myLinkedList.printList();
myLinkedList.append(5);
myLinkedList.printList();
myLinkedList.append(15);
myLinkedList.printList();
myLinkedList.append(20);
myLinkedList.printList();
// -----------------------
myLinkedList.prepend(1);
myLinkedList.printList();
myLinkedList.prepend(0);
myLinkedList.printList();
myLinkedList.insert(2, 12);
myLinkedList.printList();
myLinkedList.insert(3, 13);
myLinkedList.printList();
myLinkedList.insert(0, 21);
myLinkedList.printList();
myLinkedList.insert(200, 25);
myLinkedList.printList();
console.log(myLinkedList.length);
// myLinkedList.remove(0);
// myLinkedList.printList();
myLinkedList.remove(3);
myLinkedList.printList();
console.log(myLinkedList.length);
// let newLL = myLinkedList.reverse();
// newLL.printList();
myLinkedList.reverse2();
myLinkedList.printList();