From d9ab18cd27cd90402ca0b18a48ae96bcc9452f8c Mon Sep 17 00:00:00 2001 From: TheKingSombrero Date: Thu, 17 Oct 2019 23:16:56 +0300 Subject: [PATCH 1/2] Fix LinkedList removeAt() 1.added base case for removeAt(1) 2.switched assigment on line #138 where previous and current where pointing to the same node --- src/_DataStructures_/LinkedList/index.js | 49 +++++++++++++----------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index e7964c01..7e48ee03 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -122,31 +122,34 @@ class LinkedList { } removeAt(index) { - if (!this.head) { - return null; - } - - if (index >= this.length()) { - return this.removeFromEnd(); + if (!this.head) { + return null; + } + if (index==1){ + this.head=this.head.next + this.size -= 1; + return + } + if (index >= this.size) { + return this.removeFromEnd(); + + } + + let address = this.head; + let previous = address; + let count = index; + + while (count!=1) { + previous = address; + address = address.next; + count -= 1; + } + + previous.next = address.next; + console.log(address.next) + this.size -= 1; } - let address = this.head; - let previous = address; - let count = index; - - while (count) { - address = address.next; - previous = address; - count -= 1; - } - - const node = address; - previous.next = address.next.next; - this.size -= 1; - node.next = null; - return node; - } - length() { return this.size; } From 396c14134f8cf3a178257d94cc1d60a13f48d418 Mon Sep 17 00:00:00 2001 From: TheKingSombrero Date: Fri, 18 Oct 2019 21:52:54 +0300 Subject: [PATCH 2/2] Fix removeAt revision passes eslint and matches and enforced strict equality for some reason it wasn't running in node with it --- src/_DataStructures_/LinkedList/index.js | 54 ++++++++++++------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index 7e48ee03..74765c00 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -121,34 +121,34 @@ class LinkedList { return node; } - removeAt(index) { - if (!this.head) { - return null; - } - if (index==1){ - this.head=this.head.next - this.size -= 1; - return - } - if (index >= this.size) { - return this.removeFromEnd(); - - } - - let address = this.head; - let previous = address; - let count = index; - - while (count!=1) { - previous = address; - address = address.next; - count -= 1; - } - - previous.next = address.next; - console.log(address.next) - this.size -= 1; + removeAt (index) { + if (!this.head) { + return null + } + if (index === 0) { + let returnNode=this.head; + this.head=this.head.next; + this.size -= 1; + return returnNode; } + if (index >= this.size) { + return this.removeFromEnd(); + } + + let address = this.head; + let previous = address; + let count = index; + + while (count !== 1) { + previous = address; + address = address.next; + count -= 1; + } + + previous.next = address.next; + this.size -= 1; + return address; + } length() { return this.size;