Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"env": {
"browser": true,
"node": true,
"commonjs": true,
"jest": true,
"es6": true
},
"globals": {
"err": true,
"req": true,
"res": true,
"next": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": ["error", "single", { "allowTemplateLiterals": true }],
"comma-dangle": ["error", "always-multiline"],
"semi": [ "error", "always" ]
}
}

Binary file added 20200204_140847.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Table of Contents
[1- code challange class 2 array shift ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/3)
# k-th value from the end of a linked list
## challange summary
create a linked list then create a method that do what supposed to do then test it

## Challenge description
Write a method for the Linked List class which takes a number, k, as a parameter. Return the node’s value that is k from the end of the linked list. You have access to the Node class and all the properties on the Linked List class as well as the methods created in previous challenges.

## Approach & Efficiency
Write a method for the Linked List class which takes a number, k, as a parameter. Return the node’s value that is k from the end of the linked list.
## Solution

![UML](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/blob/ll-kth-from-end/20200204_140847.jpg)

[2- code challange class 3 array binary search ](https://github.com/Goorob-401-advanced-javascript/data-structures-and-algorithms/pull/2)

[
50 changes: 50 additions & 0 deletions challanges/ll-kth-from-end/ll-kth-from-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

class Node {
constructor(value) {
this.value = value;
this.next = null;
}

}

class LinkedList {
constructor() {
this.head = null;
}

append (value) {
let node = new Node(value);
if(!this.head){
this.head = node;
return this;
}
let currentNode = this.head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
return this;
}

kthFromEnd(k){
let currentNode = this.head;
let length = 0;
while(currentNode){
length++;
currentNode = currentNode.next;
}
let i = length - 1 - k;
if(i < 0 || k < 0){
return 'exception';
}
currentNode = this.head;
while(i > 0){
i--;
currentNode = currentNode.next;
}
return currentNode.value;
}

}
module.exports = LinkedList;
54 changes: 54 additions & 0 deletions challanges/ll-kth-from-end/ll-kth-from-end.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';
const LinkedList = require('./ll-kth-from-end.js');
describe('Linked list Module',()=>{
it ('constructor',()=>{
let list = new LinkedList();
expect(list.head).toBeNull();

});
it('append ()',()=>{
let ll = new LinkedList();
let initVal = '1';
ll.append(initVal);
expect(ll.head.value).toEqual(initVal);
expect(ll.head.value.next).toBeAnObject;
let secondVal = '2';
ll.append(secondVal);
expect(ll.head.value).toEqual(initVal);
expect(ll.head.next.value).toEqual(secondVal);

});
it('Where k is greater than the length of the linked list',()=>{
let ll = new LinkedList();
ll.append(1);
ll.append(3);
expect(ll.kthFromEnd(5)).toMatch('exception');

});
it('Where k and the length of the list are the same',()=>{
let ll = new LinkedList();
ll.append(1);
ll.append(3);
expect(ll.kthFromEnd(2)).toMatch('exception');
});
it('Where k is not a positive integer',()=>{
let ll = new LinkedList();
ll.append(1);
ll.append(3);
expect(ll.kthFromEnd(-2)).toMatch('exception');
});
it('Where the linked list is of a size 1',()=>{
let ll = new LinkedList();
ll.append(5);
expect(ll.kthFromEnd(1)).toMatch('exception');
});
it('“Happy Path” where k is not at the end, but somewhere in the middle of the linked list',()=>{
let list = new LinkedList();
list.append(1);
list.append(3);
list.append('5');
list.append(7);
list.append(9);
expect(list.kthFromEnd(2)).toMatch('5');
});
});
Loading