From d9ab18cd27cd90402ca0b18a48ae96bcc9452f8c Mon Sep 17 00:00:00 2001 From: TheKingSombrero Date: Thu, 17 Oct 2019 23:16:56 +0300 Subject: [PATCH 1/3] 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 9084503e81f394db62425356c07fdb51f7e2c465 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Fri, 18 Oct 2019 13:15:37 +0530 Subject: [PATCH 2/3] fix: removeAt() work as an array and returns a Node --- src/_DataStructures_/LinkedList/index.js | 81 ++++++++++++++++-------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index 7e48ee03..87a75df2 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -122,33 +122,38 @@ class LinkedList { } 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; + if (!this.head) { + return null; + } + if (index === 0) { + const node = this.head; + this.head = this.head.next; + this.size -= 1; + // set the next of the node null + node.next = null; + return node; + } + + if (index >= this.size - 1) { + return this.removeFromEnd(); + } + + let address = this.head; + let previous = address; + let count = index; + + while (count >= 1) { + previous = address; + address = address.next; + count -= 1; } + const node = previous.next; + previous.next = address.next; + this.size -= 1; + + node.next = null; + return node; + } length() { return this.size; @@ -159,6 +164,30 @@ class LinkedList { this.tail = this.head; this.size = 0; } + + traverseList() { + const arr = []; + let node = this.head; + while (node !== null) { + arr.push(node.data); + node = node.next; + } + return arr; + } } +// const ll = new LinkedList(); +// ll.addAtBeginning(20); +// ll.addAtBeginning(15); +// ll.addAtBeginning(10); +// ll.addAtBeginning(5); + +// console.log(ll.traverseList()); + +// console.log(ll.removeAt(0)); +// console.log(ll.traverseList()); + +// console.log(ll.removeAt(1)); +// console.log(ll.traverseList()); + module.exports = { LinkedList, Node }; From 346d9f00ee176205bdadee8dcd09e93b33b1f0e9 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Mon, 11 Nov 2019 08:17:25 +0530 Subject: [PATCH 3/3] fix: requested changes for LL removeAt() --- src/_DataStructures_/LinkedList/index.js | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index 87a75df2..3406b602 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -126,12 +126,7 @@ class LinkedList { return null; } if (index === 0) { - const node = this.head; - this.head = this.head.next; - this.size -= 1; - // set the next of the node null - node.next = null; - return node; + return this.removeFromBeginning(); } if (index >= this.size - 1) { @@ -147,7 +142,7 @@ class LinkedList { address = address.next; count -= 1; } - const node = previous.next; + const node = address; previous.next = address.next; this.size -= 1;