diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list.english.md
index 2d843ecaea..8829dcf4a9 100644
--- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list.english.md
+++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list.english.md
@@ -92,6 +92,61 @@ function LinkedList() {
```js
-// solution required
+function LinkedList() {
+ var length = 0;
+ var head = null;
+
+ var Node = function(element){
+ this.element = element;
+ this.next = null;
+ };
+
+ this.size = function(){
+ return length;
+ };
+
+ this.head = function(){
+ return head;
+ };
+
+ this.add = function(element){
+ var node = new Node(element);
+ if (head === null){
+ head = node;
+ } else {
+ var currentNode = head;
+
+ while (currentNode.next) {
+ currentNode = currentNode.next;
+ }
+
+ currentNode.next = node;
+ }
+ length++;
+ };
+ this.addAt = function (index, element) {
+ if (index > length || index < 0) {
+ return false;
+ }
+ var newNode = new Node(element);
+ var currentNode = head;
+ if (index === 0) {
+ head = newNode;
+ } else {
+ var previousNode = null;
+ var i = 0;
+ while (currentNode && i < index) {
+ previousNode = currentNode;
+ currentNode = currentNode.next;
+ i++;
+ }
+ previousNode.next = newNode;
+ }
+ newNode.next = currentNode;
+ length++;
+ }
+}
+
+
```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md
index 1a304f7eda..d14a7f00fa 100644
--- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md
+++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree.english.md
@@ -100,6 +100,24 @@ BinarySearchTree.prototype = {
```js
-// solution required
+var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
+function Node(value) {
+ this.value = value;
+ this.left = null;
+ this.right = null;
+}
+function BinarySearchTree() {
+ this.root = null;
+ this.isPresent = function (value) {
+ var current = this.root
+ while (current) {
+ if (value === current.value) {
+ return true;
+ }
+ current = value < current.value ? current.left : current.right;
+ }
+ return false;
+ }
+}
```
diff --git a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md
index a0d25075ab..1f7c9a17ec 100644
--- a/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md
+++ b/curriculum/challenges/english/08-coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index.english.md
@@ -9,7 +9,7 @@ challengeType: 1
Before we move on to another data structure, let's get a couple of last bits of practice with linked lists.
Let's write a removeAt
method that removes the element
at a given index
. The method should be called removeAt(index)
. To remove an element
at a certain index
, we'll need to keep a running count of each node as we move along the linked list.
A common technique used to iterate through the elements of a linked list involves a 'runner', or sentinel, that 'points' at the nodes that your code is comparing. In our case, starting at the head
of our list, we start with a currentIndex
variable that starts at 0
. The currentIndex
should increment by one for each node we pass.
-Just like our remove(element)
method, we need to be careful not to orphan the rest of our list when we remove the node in our removeAt(index) method. We keep our nodes contiguous by making sure that the node that has reference to the removed node has a reference to the next node.
+Just like our remove(element)
method, which we covered in a previous lesson, we need to be careful not to orphan the rest of our list when we remove the node in our removeAt(index)
method. We keep our nodes contiguous by making sure that the node that has reference to the removed node has a reference to the next node.
## Instructions
@@ -79,23 +79,6 @@ function LinkedList() {
length++;
};
- this.remove = function(element){
- var currentNode = head;
- var previousNode;
- if(currentNode.element === element){
- head = currentNode.next;
- } else {
- while(currentNode.element !== element) {
- previousNode = currentNode;
- currentNode = currentNode.next;
- }
-
- previousNode.next = currentNode.next;
- }
-
- length --;
- };
-
// Only change code below this line
// Only change code above this line
@@ -112,6 +95,68 @@ function LinkedList() {
```js
-// solution required
+
+
+function LinkedList() {
+ var length = 0;
+ var head = null;
+
+ var Node = function (element) { // {1}
+ this.element = element;
+ this.next = null;
+ };
+
+ this.size = function () {
+ return length;
+ };
+
+ this.head = function () {
+ return head;
+ };
+
+ this.add = function (element) {
+ var node = new Node(element);
+ if (head === null) {
+ head = node;
+ } else {
+ var currentNode = head;
+
+ while (currentNode.next) {
+ currentNode = currentNode.next;
+ }
+
+ currentNode.next = node;
+ }
+
+ length++;
+ };
+
+ this.removeAt = function (index) {
+ var currentNode = head;
+ var previous = head;
+ var count = 0;
+ if (index >= length || index < 0) {
+ return null;
+ }
+ if (index === 0) {
+ var removed = head.element;
+ head = currentNode.next;
+ length--;
+ return removed;
+ } else {
+ while (count < index) {
+ previous = currentNode;
+ currentNode = currentNode.next;
+ count++;
+ }
+ var removed = previous.next.element;
+ previous.next = currentNode.next;
+ length--;
+ return removed;
+ }
+ };
+}
+
+
```