Added solutions for 3 challenges in English / data-structures (#35825)

* Solution for add-elements-at-a-specific-index-in-a-linked-list.english.md

* Solution check-if-an-element-is-present-in-a-binary-search-tree.english.md

* Solution for remove-elements-from-a-linked-list-by-index.english.md

* change indent level to 2 space for each level

* change indent level to 2 space

* change indent level to 2 space

* delete unnecessary new lines

* keep the same seed format , and delete unnecessary new lines

* delete unnecessary new lines

* fix: added review suggestions

* fix: Added link and code tags
This commit is contained in:
RIHANE ZAKARIA
2019-06-11 00:17:11 +02:00
committed by Manish Giri
parent a893cb43c3
commit d86951f9c1
3 changed files with 139 additions and 21 deletions

View File

@ -92,6 +92,61 @@ function LinkedList() {
<section id='solution'>
```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++;
}
}
```
</section>

View File

@ -100,6 +100,24 @@ BinarySearchTree.prototype = {
<section id='solution'>
```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;
}
}
```
</section>

View File

@ -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 <code>removeAt</code> method that removes the <code>element</code> at a given <code>index</code>. The method should be called <code>removeAt(index)</code>. To remove an <code>element</code> at a certain <code>index</code>, 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 <dfn>'runner'</dfn>, or sentinel, that 'points' at the nodes that your code is comparing. In our case, starting at the <code>head</code> of our list, we start with a <code>currentIndex</code> variable that starts at <code>0</code>. The <code>currentIndex</code> should increment by one for each node we pass.
Just like our <code>remove(element)</code> 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 <code>remove(element)</code> method, which <a href="learn/coding-interview-prep/data-structures/remove-elements-from-a-linked-list" target="_blank">we covered in a previous lesson</a>, we need to be careful not to orphan the rest of our list when we remove the node in our <code>removeAt(index)</code> 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.
</section>
## 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() {
<section id='solution'>
```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;
}
};
}
```
</section>