diff --git a/algorithms/JavaScript/src/linked-lists/singly.js b/algorithms/JavaScript/src/linked-lists/singly.js index 3a42dd71..f6641e25 100644 --- a/algorithms/JavaScript/src/linked-lists/singly.js +++ b/algorithms/JavaScript/src/linked-lists/singly.js @@ -10,6 +10,39 @@ class SinglyLinkedList { this.head = null; } + // Will return the element at that particular index in linklist starting + // from head. + getByIndex(idx) { + if (!this.head) { + return 'List is Empty'; + } + + let currentNode = this.head; + let iterationCount = 0; + + for ( + currentNode = this.head; + currentNode != null; + currentNode = currentNode.next + ) { + if (iterationCount == idx) { + return currentNode; + } + iterationCount++; + } + + return null; + } + + setByIndex(idx, newValue) { + const nodeElem = this.getByIndex(idx); + if (nodeElem) { + nodeElem.data = newValue; + return nodeElem; + } + + return null; + } insertAtHead(data) { const newNode = new Node(data); if (this.head == null) { @@ -20,6 +53,47 @@ class SinglyLinkedList { } } + // idx : index of the node after which you want to + // insert the new node; + // data: value associated with new node. + insertAfterByIndex(idx, data) { + let afterNode; + let newNode; + + const beforeNode = this.getByIndex(idx); + + if (beforeNode) { + afterNode = beforeNode.next; + newNode = new Node(data); + + beforeNode.next = newNode; + newNode.next = afterNode; + + return this; + } + + return null; + } + + // idx: index of the node which you want to remove from linklist. + removeFromMiddle(idx) { + const beforeNode = this.getByIndex(idx - 1); + let afterNode; + let nodeToRemove; + + if (beforeNode) { + nodeToRemove = beforeNode.next; + + if (nodeToRemove) { + afterNode = nodeToRemove.next; + beforeNode.next = afterNode; + + return this; + } + } + return null; + } + removeAtHead() { // check for underflow if (this.head == null) { @@ -50,6 +124,34 @@ class SinglyLinkedList { return this; } + // Removes elements which are at the end of the linklist. + removeFromEnd() { + if (!this.head) { + console.log('List is Empty'); + return this; + } + + let returnNode; + if (!this.head.next) { + returnNode = this.head; + this.head = null; + return returnNode; + } + + let currentNode = this.head.next; + let previousNode = this.head; + + while (currentNode) { + if (currentNode.next) { + previousNode = currentNode; + } + currentNode = currentNode.next; + } + returnNode = previousNode.next; + previousNode.next = null; + return returnNode; + } + printList() { // if head is null then list is empty if (this.head == null) { @@ -71,5 +173,15 @@ array.insertAtHead('xyz'); array.insertAtHead(1.1); array.removeAtHead(); array.insertAtEnd(99); +array.insertAtEnd(100); +array.insertAtEnd(101); +array.insertAtEnd(102); +array.insertAtEnd(103); +array.insertAtEnd(104); +array.insertAtEnd(105); + array.printList(); +array.removeFromEnd(); + +array.printList();