diff --git a/src/_DataStructures_/LinkedList/index.js b/src/_DataStructures_/LinkedList/index.js index e7964c01..74765c00 100644 --- a/src/_DataStructures_/LinkedList/index.js +++ b/src/_DataStructures_/LinkedList/index.js @@ -121,12 +121,17 @@ class LinkedList { return node; } - removeAt(index) { + removeAt (index) { if (!this.head) { - return null; + return null } - - if (index >= this.length()) { + if (index === 0) { + let returnNode=this.head; + this.head=this.head.next; + this.size -= 1; + return returnNode; + } + if (index >= this.size) { return this.removeFromEnd(); } @@ -134,17 +139,15 @@ class LinkedList { let previous = address; let count = index; - while (count) { - address = address.next; + while (count !== 1) { previous = address; + address = address.next; count -= 1; } - const node = address; - previous.next = address.next.next; + previous.next = address.next; this.size -= 1; - node.next = null; - return node; + return address; } length() {